for (;;){
try{
int a=input.nextInt();
int b=input.nextInt();
try{
if (a/b>2){
System.out.println("Done!");
break;
}else{
System.out.println("please input again");
}
}
catch(Exception e){
System.out.println("if is wrong");
}
}
catch(Exception e){
System.out.println("input is wrong");
continue;//加不加不影响
}
}这段代码在执行时,如果输入1和2,则会继续执行;输入2和0,会报错并继续要求输入;但是如果输了1.5之类的违规数字,则会直接无限循环报错"input is wrong"而不是继续要求执行输入。用debug检查发现每次执行到输入就自动跳到最后了。请问为什么?
自己已经解决,需要将Scanner input=new Scanner(System.in);放到try里面才行,不然input已经是定值了