• Post Reply Bookmark Topic Watch Topic
  • New Topic

New Help on letting the user to choose to play again or not. here is what i have  RSS feed

 
Jeremy K Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont know why this wont work.

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the JavaRanch.

First of all you need to tell us what you want it to do & what is actually happening. Basically TellTheDetails.
 
Jeremy K Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is what I want it to do

*The computer selects one number randomly between 1 and 100 inclusive. This is the 'answer'.
* The user makes a guess.
* The computer decides if the guess is correct, higher than the answer (too high) or lower (too low) and informs the user.
* The user keeps guessing until they get it right.
* The computer tells the user how many guesses they made.
* The user chooses to play again or not

I cant get the program to run the last if statement and rerun the program when user selects yes to want to play again.

right now i enter a number and it comes out 1 guess, then I enter another number and its does nothing, then i enter a third number and it says 2 guesses.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your indenting tells another story than your brackets do
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got so much code in your main method?

If you simply want to repeat the game if the user enters "yes", you will have to put your code inside a loop. You can have a loop which runs until the user enters "stop" or you can have a loop which repeats only when the user enters "yes". Or similar. There is a peculiar idiom, which I shall tell you about, because you will never guess it by yourself.I am sure you will get it quicker than somebody did three years ago. That was a problem similar to yours, only the poster wanted to stop when the user entered "stop". Note a different String class method from equals() was recommended.
 
Jeremy K Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have it in a Do while loop, and I have the code to repeat again if the user selects yes. but it is not working.

else if (guess == target)
System.out.println("you are correct! do you want to play again? yes or no.");
answer = keyboard.next();

System.out.println (count + " Guesses!");

count++;
}while((guess != target) || (answer.equalsIgnoreCase("yes")));

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you need two loops, one for the game itself, and the other for repeating the game.
I would also suggest you get into the habit of always using {} for if blocks, etc. What you have is an if block which prints congratulations if you win, but takes answer regardless. That is because you have omitted the {}, so the compiler only sees one line as being "inside" the if. Try adding {} after the if (guess == target) bit, and see whether that makes any difference.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!