public int inputAge(){
int result = -1;
Scanner scan = new Scanner(System.in) ;
while(true){
try{
//scan = new Scanner(System.in);//这样的话就不会发生死循环
result = scan.nextInt();//这里是读取整数,你输入非数字当然会出错咯
if(result <=0 || result >= 150){
Exception me = new Exception("年龄超出合理范围");
throw me;
}
break;
}catch(Exception e){
System.out.println("请重新输入:");//出错了,就输出这个
continue;//continue是继续,所以会出现死循环啦
}
}
return result;
}
int result = -1;
Scanner scan = new Scanner(System.in) ;
while(true){
try{
//scan = new Scanner(System.in);//这样的话就不会发生死循环
result = scan.nextInt();//这里是读取整数,你输入非数字当然会出错咯
if(result <=0 || result >= 150){
Exception me = new Exception("年龄超出合理范围");
throw me;
}
break;
}catch(Exception e){
System.out.println("请重新输入:");//出错了,就输出这个
continue;//continue是继续,所以会出现死循环啦
}
}
return result;
}
因为你又建了个输入对象,但出错的对象还是处于死循环中,你看到的是新的对象,而不是原来那个出错的