How to keep looping with try and catch to get correct input?
问题 I'm trying to make a function that checks for an integer and will keep looping until the user correctly enters an integer of 17 or higher. However, if I put in wrong input, like 'K', or '&', it will get stuck in an infinite loop. public static int getAge(Scanner scanner) { int age; boolean repeat = true; while (repeat) { try { System.out.println("Enter the soldier's age: "); age = scanner.nextInt(); repeat = false; } catch(InputMismatchException exception) { System.out.println("ERROR: You