Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Java Would You like to play again?

 
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

 
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.
 
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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic