2

I need to create a simple game where a random number is created, and the user has to guess the number by inputting numbers into a scanner. If their guess is too high, the system tells them to guess lower, and the same if it is too low.

I'm using a while loop, but I don't know how to continuously call the scanner so that the user can keep guessing. Here is my code so far:

public static void highLow()
{
  Random randomGenerator = new Random();
  int num = randomGenerator.nextInt(100);
  boolean loop = true;

  while(loop)
  {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Please enter a number: ");
    int numGuess  = scanner.nextInt();
        if (numGuess > num)
          System.out.println("Guess lower!");
          scanner.nextInt();

        if (numGuess < num)
          System.out.println("Guess higher!");
          scanner.nextInt();

        if (numGuess == num)
          System.out.println("Correct! You win!!!");
          loop = false;
  }
}
Gaël Barbin
  • 3,769
  • 3
  • 25
  • 52
user4817285
  • 25
  • 1
  • 4
  • possible duplicate of [how can i use java scanner in while loop](http://stackoverflow.com/questions/13284949/how-can-i-use-java-scanner-in-while-loop) – Nate Barbettini Apr 22 '15 at 01:59
  • Welcome to Stack Overflow! FYI, if you're learning Java, you might want to check out [codereview.se] where you can get lots of tips for improvement. – Nate Barbettini Apr 22 '15 at 02:07

1 Answers1

1

You're almost there. Here are some suggestions:

  1. Assign the input of the Scanner back into numGuess in order to have the value "ready" for the next loop iteration.
  2. Use braces after your if conditions.
  3. You don't have to redeclare the Scanner object each time. Do it once, before the loop.

It was a combination of #1 and #2 that was causing your code to fail before. Look closely at your if blocks. If you leave off the braces, only the first line following is part of the if. (see Is it ok if I omit curly braces in Java?)

So, you meant this:

if (numGuess == num)
    System.out.println("Correct! You win!!!");
    loop = false;

But what the compiler "saw" was really this:

if (numGuess == num)
    System.out.println("Correct! You win!!!");
loop = false;

The misplaced loop = false will ensure that your loop only ever runs once, no matter what the user entered. Explicitly including braces keeps it unambiguous!

Here's what your code looks like after making the above changes:

public static void highLow()
{
    Random randomGenerator = new Random();
    int num = randomGenerator.nextInt(100);
    boolean loop = true;
    Scanner scanner = new Scanner(System.in);
    int numGuess = 0;

    while(loop)
    {
        System.out.print("Please enter a number: ");
        numGuess  = scanner.nextInt();
        if (numGuess > num) {
            System.out.println("Guess lower!");
            numGuess  = scanner.nextInt();
        } else if (numGuess < num) {
            System.out.println("Guess higher!");
            numGuess  = scanner.nextInt();
        } else if (numGuess == num) {
            System.out.println("Correct! You win!!!");
            loop = false;
        }
    }
}
Community
  • 1
  • 1
Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
  • Thank you very much, this fixed a lot of issues. However, now when I first input a number, the scanner says for example "Guess lower!", with a scanner box underneath. I input another number, and instead of registering, it loops back to the beginning and asks me to input a number. Is there any way to do this with only 1 scanner box appearing? – user4817285 Apr 22 '15 at 02:12
  • Try moving the first `System.out.print` line from inside the loop to just before the loop, so it only is executed once. – Nate Barbettini Apr 22 '15 at 02:14
  • It still looks like this... I have to put in the number twice for it to register Guess higher! [DrJava Input Box] 7 [DrJava Input Box] 7 Guess lower! – user4817285 Apr 22 '15 at 02:20
  • Oops, sorry, in my previous comment I meant to say: move the first *two* lines of the loop outside. :) – Nate Barbettini Apr 22 '15 at 02:21
  • ahhhhhh... That makes much more sense! Thanks for your help! :D – user4817285 Apr 22 '15 at 02:23
  • No problem! Glad to help! Here's an upvote for you, so you can get closer to upvoting my answer as well ;) – Nate Barbettini Apr 22 '15 at 02:25