while try catch无限循环


   
Scanner console= new Scanner(System.in); 
 private int CatchInput() {
while (true) {
try {
System.out.println("------输入id------");
return console.nextInt();
} catch (InputMismatchException e) {
System.out.println("------输入错误------");
}
}
}
当console类型输入错误 执行catch语句 通过while循环重新执行try语句 会使用上一次的结果直接进入catch 以此往复无限循环


 
 private int CatchInput() {
while (true) {
try {
          Scanner console= new Scanner(System.in);  
                System.out.println("------输入id------");
return console.nextInt();
} catch (InputMismatchException e) {
System.out.println("------输入错误------");
}
}
}
将Scanner的定义放入try语句中即可


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM