flag = true;
int x;
int y;
while(flag){
try{
System.out.print("输入X值:");
x = input.nextInt();
System.out.print("输入Y值:");
y = input.nextInt();
flag = false;
ic.operation(x,y);
}catch(InputMismatchException e){
System.out.println("输入数超出范围,请重新输入");
}
finally{
}
}
int x;
int y;
while(flag){
try{
System.out.print("输入X值:");
x = input.nextInt();
System.out.print("输入Y值:");
y = input.nextInt();
flag = false;
ic.operation(x,y);
}catch(InputMismatchException e){
System.out.println("输入数超出范围,请重新输入");
}
finally{
}
}
在catch加一句 input.next();清除上次错误就能继续执行了。public class test10 { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
boolean flag = true;
int x;
int y;
while (flag) {
try {
System.out.print("输入X值:");
x = input.nextInt();
System.out.print("输入Y值:");
y = input.nextInt();
flag = false;
} catch (InputMismatchException e) {
System.out.println("输入数超出范围,请重新输入");
input.next();
} } }}运行结果:输入X值:aaa
输入数超出范围,请重新输入
输入X值:
或者清掉错误的输入,在catch里面加上input.next();