next();出现错误时,你需要致电。另外建议使用
hasNextInt()
catch (Exception e) { System.out.println("Error!");input.next();// Move to next other wise exception }在读取整数值之前,你需要确保扫描仪具有一个。而且你不需要像这样的异常处理。
Scanner scanner = new Scanner(System.in); int n1 = 0, n2 = 0; boolean bError = true; while (bError) { if (scanner.hasNextInt()) n1 = scanner.nextInt(); else { scanner.next(); continue; } if (scanner.hasNextInt()) n2 = scanner.nextInt(); else { scanner.next(); continue; } bError = false; } System.out.println(n1); System.out.println(n2);


