I am trying to make a program where the user will input a random integer/number. But the main issue is, if users accidentally input a String, I want them to be informed and also I want them to ask to input again by asking them again. But in my code, I cant loop my catch.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int answer = 28;
int attempts = 0;
System.out.println("Guess a Number 1 - 50:");
while(true) {
try {
int input = scan.nextInt();
scan.nextLine();
if(input > answer) {
System.out.println("Too Big");
System.out.println("Guess a Number 1 - 50:");
} else if (input < answer) {
System.out.println("Too Small");
System.out.println("Guess a Number 1 - 50:");
} else if (input == answer ) {
System.out.println("Congrats!");
}
} catch (InputMismatchException e) {
System.out.println("Numbers only");
System.out.println("Guess a Number 1 - 50:");
}
}
}