Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java do loop - string cannot be resolved

Working on a simple dice rolling game using programmer defined methods in Java. Since I know the program needs to run at least once and then ask if the player wants to go again, I elected to use a do/while loop. I've never had to use one before, but I believe I set it up right.

My code is as follows, excluding the body of my main method contained within the loop, as well as the programmer defined methods.

do {
    // main METHOD BODY ..

    String proceed = ui.nextLine();
    System.out.print("Would you like to play again (yes/no)? ");

} while (proceed.charAt(0) == 'Y' || proceed.charAt(0) == 'y');

What I get from the Eclipse IDE at this point is "proceed cannot be resolved."

Any ideas as to what's going on?

like image 826
awfulwaffle Avatar asked Jan 29 '26 06:01

awfulwaffle


1 Answers

proceed isn't in scope at the time it is being dereferenced in the while loop. Move the declaration of the proceed variable outside of the do.

String proceed;
do{

 main METHOD BODY

    proceed = ui.nextLine();
    System.out.print("Would you like to play again (yes/no)? ");

} while (proceed.charAt(0) == 'Y' || proceed.charAt(0) == 'y');
like image 77
Eric Rosenberg Avatar answered Jan 30 '26 19:01

Eric Rosenberg