• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
  • Tim Holloway
  • Carey Brown
  • salvin francis

Java Would You like to play again?

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

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.
Posts: 1090
  • 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.

Posts: 10777
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michal Hartwich wrote:I need help.


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.


Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
create, convert, edit or print DOC and DOCX in Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!