嘗試接受 [0,1,2,3,4] 范圍內(nèi)的用戶輸入,直到他輸入有效數(shù)字。使用try-catch修復異常錯誤 boolean good = true; int x = reader.nextInt(); while (good != false) { try { if (x > launchEventList.size()-1) { System.out.println("Please provide a valid input"); reader.nextInt(); } else { System.out.println("\nDetails of event on index "+ x + " are ==>> " + getLe(x)); good = false; } } catch (IndexOutOfBoundsException e) { System.out.println("Invalid Input"); }
添加回答
舉報
0/150
提交
取消