1

I can't seem to be able have my program ask the user if they would like to play again. The program ends when asking if they would like to continue. I added a break because before the program was continuously looping the correct answer and number of tries.

import java.util.Random;
import java.util.Scanner;

public class GuessTheNumber {
      public static void main(String[] args) {

              boolean play = true;
              Scanner input = new Scanner(System.in);
              Random Number = new Random();
              int GuessNumber = 1+ Number.nextInt(1000);
              int guess = 0;
              int Tries = 0;

              while(play) {
                      while(guess != GuessNumber) {
                              System.out.println("Please Enter a number between 1 and 1000:");
                              guess = input.nextInt();
                              Tries++;

                              if(guess == GuessNumber) {
                                      System.out.println("You Win!");
                                      break;
                              }
                              else if(guess < GuessNumber) {
                                      System.out.println("Guess is too low");
                              }
                              else if(guess > GuessNumber) {
                                      System.out.println("Guess is too high");
                              }
                      }
                      System.out.printf("Number was: %d",GuessNumber);
                      System.out.println("");
                      System.out.printf("Number of tries was: %d ",Tries);
                      System.out.println("");
                      System.out.println("Would you like to play again?(Yes/No)");
                      String playagain = input.nextLine();
                      if("Yes".equals(playagain))
                              play = true;
                      else
                              play = false;
              }
      }
}
wadda_wadda
  • 846
  • 1
  • 7
  • 26
nahoc44
  • 19
  • 3
  • 1
    use if(playagain.equals("Yes") – Burak Karasoy Nov 01 '15 at 23:50
  • See [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods). – TNT Nov 01 '15 at 23:51

1 Answers1

0

A few things with your program.

As the comments have noted, you do need to flush your new-line character after using nextInt().

But, you must also set the guess value and try-count back to 0 after the user states that they would like to play again -- otherwise the program will just continue in a "You win!" loop.

I would recommend moving your variable declarations to the outer loop:

while(play) { 
  int guess = 0;
  int guessNumber = 1 + Number.nextInt(1000);
  int tries = 0;
  ...
}
wadda_wadda
  • 846
  • 1
  • 7
  • 26
  • I did both of those things but should I also find a new random number? The program just says "You win" with zero tries and the same number. – nahoc44 Nov 02 '15 at 00:02