I'm new here and I'm learning Java. In one of my programs, I have made a guessing game. The guessing game is supposed to keep asking the user to input a guess until they guess the number right.
This is my code:
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
final int minValue = 1;
final int maxValue = 10;
final boolean displayHints = true; // Display whether the number is too high or too low when guessed incorrectly?
int tries = 1;
int guess = 0; // We need to give 'guess' a (temporary) value or else the 'while' loop will create an error
boolean error = false;
Random generator = new Random(); // Create scanner 'generator'
int random = generator.nextInt(maxValue) + minValue; // Define 'random' variable with a random value
if (random == guess) { // In case 'random' = 'guess'
guess = -852654;
}
Scanner input = new Scanner(System.in); // Create a scanner
System.out.println("Random number: " + random); // Hey, no cheating! (for debugging purposes)
System.out.println("Try to guess the magic number! (from " + minValue + " to " + maxValue + ")");
while (random != guess) {
do { // Supposed to ask the user to input a number until they enter a valid number. This is the part of the code that is not working.
System.out.println("\nInput your guess now!");
try {
guess = input.nextInt();
error = false;
} catch (InputMismatchException e) {
System.err.println("That's not a number!\n");
error = true;
continue;
}
} while (error);
if (guess == random) {
System.out.println("Correct!");
System.out.println("Number of tries: " + tries + ".");
input.close();
} else {
tries++;
if (displayHints) {
if (guess < random) {
System.out.println("Sorry, too low!");
} else if (guess > random) { // not strictly necessary
System.out.println("Sorry, too high!");
}
} else {
System.out.println("Sorry, that was not the right number");
}
}
}
}
}
The code is pretty self-explanatory, because I made a lot of comments. The problem, though, is when the user enters an invalid integer (like 'banana'), instead of saying "That's not a number!" and asking for another number, the code does something like this:
Random number: 9
Try to guess the magic number! (from 1 to 10)
Input your guess now!
banana
Input your guess now!
Input your guess now!
Input your guess now!
Input your guess now!
Input your guess now!
Input your guess now!
Input your guess now!
Input your guess now!
Input your guess now!
Input your guess now!
That's not a number!
Input your guess now!
That's not a number!
That's not a number!
That's not a number!
That's not a number!
That's not a number!
That's not a number!
That's not a number!
That's not a number!
That's not a number!
That's not a number!
That's not a number!
Input your guess now!
That's not a number!
Input your guess now!
That's not a number!
Input your guess now!
That's not a number!
Input your guess now!
That's not a number!
Input your guess now!
That's not a number!
Input your guess now!
That's not a number!
The rest of the code works perfectly.