mardi 21 avril 2015

Repeating/Looping scanner

Sorry if this title was bad, I'm new to this :) . 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. But I need to use a while loop, and so I don't know how to continuously call the scanner so that the user can keep guessing. Here is my code down below, and all help is appreciated! I don't know if I used scanner correctly or not.

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;
  }
}




Aucun commentaire:

Enregistrer un commentaire