Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Junky

Thread: while loop error

  1. #1
    AyuzawaM is offline Member
    Join Date
    Aug 2014
    Posts
    12
    Rep Power
    0

    Default while loop error

    So I keep getting a compiling error stating that my variable totalScore is not initialized even though it is initialized. This is happening at the part where it states totalScore >= 100 Help please? All my friend keeps saying is it isn't initialized properly but I know that so he is just stating the obvious :( Here's the program. I highlighted the part the compiler is giving me trouble..

    import java.util.Scanner;
    import java.util.Random;

    public class Pig
    {
    public static void main(String[] args)
    {

    int die; int totalScore; int turnScore;



    Scanner keyboard = new Scanner(System.in);

    Random rand = new Random();


    System.out.print("Welcome to the game of Pig!");

    while (totalScore >= 100)

    {

    int totalScore;

    turnScore = rand.nextInt(6) + 1;

    System.out.println("You rolled: " + turnScore);




    }






    {
    }

    }











    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: while loop error

    Java Code:
    int myVar;
    This declares a variable, it does not initialise it (giving it an initial value). Also be aware that you have declared 2 variables called totalScore. Did you mean to do this?

  3. #3
    AyuzawaM is offline Member
    Join Date
    Aug 2014
    Posts
    12
    Rep Power
    0

    Default Re: while loop error

    Quote Originally Posted by Junky View Post
    Java Code:
    int myVar;
    Also be aware that you have declared 2 variables called totalScore. Did you mean to do this?
    I did to see if it would help with my error, however I see the mistake now since you pointed it out

  4. #4
    Abbhirami is offline Member
    Join Date
    Jun 2014
    Posts
    5
    Rep Power
    0

    Default Re: while loop error

    hey, can you lease tell me what this program does?i really don't understand.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: while loop error

    It doesn't do much yet, it is clearly unfinished. It looks like the idea is to simulate the throwing of dice (you know: those cubes with number 1 to 6 on them often used in games of chance) for a game.
    Last edited by gimbal2; 08-22-2014 at 10:51 AM. Reason: dies = dice, corrected by a junky
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Abbhirami is offline Member
    Join Date
    Jun 2014
    Posts
    5
    Rep Power
    0

    Default Re: while loop error

    Oh okay..thanks ..

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: while loop error

    Quote Originally Posted by gimbal2 View Post
    dies
    <nitpick>
    die - singular
    dice - plural
    </nitpick>
    gimbal2 likes this.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: while loop error

    Argh, two corrections in one day! I has a sad :(
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. ConcurrentModificationException error in for loop
    By Sutured in forum New To Java
    Replies: 3
    Last Post: 03-18-2013, 11:13 PM
  2. Do While loop error
    By JeffMCincy in forum JCreator
    Replies: 1
    Last Post: 02-21-2011, 06:54 PM
  3. Loop error
    By Spyderpig in forum New To Java
    Replies: 4
    Last Post: 02-19-2011, 01:56 AM
  4. For loop error
    By ShotGunRockets in forum New To Java
    Replies: 6
    Last Post: 04-06-2009, 02:14 AM
  5. help error in while loop
    By iPetey in forum New To Java
    Replies: 3
    Last Post: 04-03-2009, 04:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •