我有这个代码
package example;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int rep;
int[] arraya = new int[2];
do {
try {
rep = 0;
System.out.print("input col :");
int kol = input.nextInt();
System.out.print("input value :");
int val = input.nextInt();
arraya[kol] = val;
} catch (InputMismatchException e) {
System.out.println("input must integer");
rep = 1;
input.next();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("out of range");
rep = 1;
}
} while (rep == 1);
}
}
为什么我必须添加input.next();
以catch(InputMismatchException e);
避免无限循环?
为什么incatch(ArrayIndexOutOfBoundsException e);
不需要input.next();
避免无限循环?
在catch(ArrayIndexOutOfBoundsException e);
,循环运行良好,但input.next();
为什么它不同于catch(InputMismatchException e);
?