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{

}

}

解决方案 »

  1.   

    看你想怎么搞,可以 break 出来,可以return停止method, 可以System.exit(1); 停止程序。 可以check x 按需求自己选择。
      

  2.   


    在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值:
      

  3.   

    楼上正解,关键是需要让scanner.next()来略过当前错误输入,否则就永远无法前进,陷入报错怪圈了。
      

  4.   

    一般情况下应该把try/catch放在循环外面,报错的话直接结束掉就好;
    或者清掉错误的输入,在catch里面加上input.next();