This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Play again function help?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!