Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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

Need help ending number guessing game and asking if user wants to play again

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a number guessing game that allows 2-5 players. There is a random number generated for each player. I want the game to end as soon as the 1st player guesses the number correctly and ask for user input if they want to play again. Previously I was able to get the game to end after the 1st player to guess correctly by using system.exit but now I want to have the option for players to choose to play again and I was hoping someone can help me.

Here is my code:



Thanks.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello willis, and welcome to the Ranch!

but now I want to have the option for players to choose to play again



Whenever you say that you want a part of a program to repeat, you should automatically think of using a loop of some kind and in fact enclosing the code that needs to repeat in this loop. In this situation where you don't know initially how many times the loop will occur, but you do know that it will occur at least one time, you should consider using a do-while loop.

Here is my code:


Your code is all left-justified and hard to read. As your goal is to get as many people as possible to read your code, you'll probably want to make it easier to read by editing this code so that it is properly indented.

Best of luck and again, welcome!

Pete
 
willis lee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I really don't have any idea what I am doing but this is what I came up with so far. Sorry my indenting is terrible, I'm not sure how to indent correctly.

All I wish to know now is how I can get this program to stop asking for guesses after the first person guesses correctly. I tried the break command but there were errors and again, I don't have much experience. I want the program to ask if the user wants to play again after the first player guesses their number correctly.



Thanks.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!