您可以使用do-while循环来消除first
input.nextInt()。
do { try { System.out.print("Enter the number of students: "); students = input.nextInt(); } catch (InputMismatchException e) { System.out.print("Invalid number of students. "); } input.nextLine(); // clears the buffer} while (students <= 0);因此,所有这些
InputMismatchException都可以在一个地方处理。



