• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Play again function help?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic