Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: do while loop, variable can't be found

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default do while loop, variable can't be found

    import java.util.*;
    public class Pig{

    public static void main (String [] args){
    int player1 = 0;
    int player2 = 0;
    String s = "";
    String b = prompt(s);
    if (b.charAt(0) =='y'){
    player1();
    player2();
    } else{
    System.out.println("Too bad maybe next time!!");
    }
    }

    //Initial Prompt
    public static String prompt (String s){
    System.out.println ("Welcome to the game of \"pig\".");
    System.out.println ("This is a two-player game.");
    System.out.println ("The first player rolls a 6-sided die.");
    System.out.println ("The player can roll as many times as she/he");
    System.out.println ("likes until she/he wishes to stop or gets a 1.");
    System.out.println ("If the first player chooses to stop, she/he");
    System.out.println ("gets the sum of all her/his rolls added to");
    System.out.println ("her/his score. If the first player stops becuase");
    System.out.println ("she/he has rolled a one, she/he gets no points");
    System.out.println ("for that turn. The first player to reach 100");
    System.out.println ("points wins the game.");
    System.out.print ("Do you want to play(y/n)? ");
    String c = scanner();
    return c;
    }

    // Turn for first player
    public static void player1 (){
    System.out.println ("PLAYER 1 ************************************ rolling...");
    do{
    int i = random();
    System.out.println("You rolled a "+i);
    System.out.print("Do you want to rolla gain? ");
    String c = scanner();
    String d = c.toLowerCase();
    } while (d.charAt(0) == 'y');
    System.out.println("A wise choice");
    }

    //Turn for second player
    public static void player2 (){
    System.out.println ("PLAYER 2 ************************************ rolling...");
    do{
    int i = random();
    System.out.println("You rolled a "+i);
    System.out.print("Do you want to rolla gain? ");
    String c = scanner();
    String d = c.toLowerCase();
    } while (d.charAt(0) == 'y');
    System.out.println("A wise choice");
    }

    //Generates numbers
    public static int random (){
    Random rand = new Random ();
    int random = rand.nextInt (6)+1;
    return random;
    }

    //Scanner input
    public static String scanner (){
    Scanner sc = new Scanner (System.in);
    String yn = sc.next();
    String s = yn.toLowerCase();
    return s;
    }
    }

    --- Update ---

    these are the error messages i received:

    ivans-mbpocuments IvanBaird$ javac Pig.java
    Pig.java:43: cannot find symbol
    symbol : variable c
    location: class Pig
    } while (c.charAt(0) == 'y');
    ^
    Pig.java:56: cannot find symbol
    symbol : variable d
    location: class Pig
    } while (d.charAt(0) == 'y');
    ^
    2 errors


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,620
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: do while loop, variable can't be found

    Did you post the errors for this code? I can't find this statement in the code:
    while (c.charAt(0) == 'y');

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: do while loop, variable can't be found

    You have declared the variables inside the do while loop. The condition statement of the do while loop is outside the loop and therefore does not have access to the variables declared inside the loop.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: do while loop, variable can't be found

    sorry i forgot to save the changes i made before trying to compile the second time.

    these are the new error codes:
    ivans-mbpocuments IvanBaird$ javac Pig.java
    Pig.java:44: cannot find symbol
    symbol : variable d
    location: class Pig
    } while (d.charAt(0) == 'y');
    ^
    Pig.java:58: cannot find symbol
    symbol : variable d
    location: class Pig
    } while (d.charAt(0) == 'y');
    ^
    2 errors
    if I'm not supposed to assign the variables inside the loop then how do i initiate the scanner repeatedly without it being in the loop.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: do while loop, variable can't be found

    Read my post again. I never mentioned assigning values to the variables.
    Improving the world one idiot at a time!

Similar Threads

  1. Please help issue with a variable in a loop.
    By Zebo999 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2013, 04:05 PM
  2. help with defining a changing variable within a loop
    By uswhovian in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 9th, 2013, 10:30 AM
  3. [SOLVED] While loop won't allow variable to change
    By Thor in forum Loops & Control Statements
    Replies: 13
    Last Post: October 9th, 2012, 08:35 PM
  4. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  5. Variable updates after loop
    By tecno40 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 9th, 2011, 04:34 AM

Tags for this Thread