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