private static String getBookByNumber(String[] books)???throws Exception {??while (true) {???System.out.println("輸入圖書序號:");???try {????//獲取輸入的圖書序號(數組下標)????int index = inputCommand();????//若返回值為-1????if(index == -1){?????System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");?????continue;????}????//若不出現”數組下標越界異?!埃瑒t返回相應位置的圖書????String book = books[index];????return book;???} catch (ArrayIndexOutOfBoundsException e) {????//輸入的序號不存在(引發”數組下標越界異?!埃瑒t拋出”圖書不存在異?!????Exception bookNotExists = new Exception("圖書不存在!");????bookNotExists.initCause(e);????throw bookNotExists;???}??}?}
添加回答
舉報
0/150
提交
取消