我面對java.util.InputMismatchException;
inputmismatchexception:進入無限循環?
我趕上InputMismatchException時,但我不明白爲什麼它會進入無限循環以第一輸入錯誤後,輸出繼續這樣:
enter two integers
exception caught
這樣下去重複
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int flag = 0;
while (flag != 1) {
try {
System.out.println("enter two integers");
int a = sc.nextInt();
int b = sc.nextInt();
int result = a + b;
flag = 1;
System.out.println("ans is" + result);
} catch (NumberFormatException e) {
System.out.println("exception caught");
} catch (InputMismatchException e) {
System.out.println("exception caught");
}
}
}
@Berger是啊,我不知道OP很希望如何進入他的數據。 –