• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Would You like to play again?  RSS feed

 
Michal Hartwich
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help.

I really don't know where should I begin with the question about playing again.

the Game is simple guessing a number and it works fine.

I just don't know what should I do to make that question about playing again work.
PLease Help

 
Lee Zh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can ask the player to confirm the answer before break out the while loop.

add question before line24: win=true;
and check the answer, then decide break out or reset the guess number.
 
Chan Ag
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.



This is not a very pleasant approach. You might get an InputMismatchException too often ( just an extra space before a 5 would give you an InputMismatchException).
If this is not what you want, you could try scanner.nextLine().trim() first.
This would format your input so leading and trailing spaces are removed. Once you have the formatted ( and hence slightly more safe ) input, you could use a parseInt. parseInt throws a NumberFormatException so you'd need to provide a NumberFormatException handler.

Coming to how do you get to ask the user to input the number again, you have to put that thing of taking the user input inside a loop so it repeats. while (true) is a good option.
Or while (!someBooleanFlag) is something you could try.

Also rather than coding as follows,



it is a better practice to code as follows.




Chan.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michal Hartwich wrote:I need help.

Michal,

Please DontWriteLongLines. It makes your thread hard to read,
and it's actually bad coding practice.
I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.

I think in your case, it was mainly due to the fact that your indentation is screwed up.

Thanks.

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