嘗試接受 [0,1,2,3,4] 范圍內的用戶輸入,直到他輸入有效數字。使用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"); }
- 1 回答
- 0 關注
- 75 瀏覽
添加回答
舉報
0/150
提交
取消