This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Play again function help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Play again function help?" Watch "Play again function help?" New topic
Author

Play again function help?

Dj Lee
Greenhorn

Joined: Apr 14, 2011
Posts: 3
I've made a hi-lo guessing game and I'm having trouble with the play again function using the loop.

Here's my code:


line 59 to 73 is where I inserted the play again function.

If the user correctly guesses the number right, the program prints "Play again? Y or N?" twice...
and I'm allowed to enter an input but it just loops through "play again? y or n?" instead of actually starting the program again.

What am I doing wrong?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
I was surprised to see the second 'do' loop. With each prompt in the main do loop, you've already instructed the user to enter -1 if he/she wants to quit. That by itself could terminate the game.

Then, your second do loop - just as you said - continues to loop, asking the player if they want to continue to play. That's all the loop does. There's no way for a true answer to get the logic back to the main do loop.

So, figure out what you meant to do with the -1 selection in the main do loop. Or, if you wan to ask the user if he/she wants to continue, that would be a line at the end of the main do loop that sets the do loop flag to continue or exit, similar to what you've done in the second do loop. You don't need both exit paths, just one of them in the main do loop.



Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
 
 
subject: Play again function help?
 
Similar Threads
Help with number guessing game
Unable to Return Variable
99 bottles of beer song.......
Trouble with Guessing Game
Need help ending number guessing game and asking if user wants to play again