感覺(jué)有點(diǎn)奇怪,好像沒(méi)有正確使用異常。
package?com.jsdx; import?java.util.Scanner; import?java.util.Arrays; public?class?RentBook? { private?static?String[]?bookName?=?{"悟空傳","斗羅大陸","斗破蒼穹","擇天記","遮天"}; public?RentBook() { } public?static?void?main(String[]?args) { RentBook?rentBook?=?new?RentBook(); rentBook.select(); } public?void?bookNumber() { System.out.println("輸入圖書(shū)序號(hào):"); @SuppressWarnings("resource") Scanner inNumber?=?new?Scanner(System.in); if(inNumber.hasNextInt()) { int?number?=?inNumber.nextInt(); if(number?>?0?&&?number?<=?bookName.length) { System.out.println("圖書(shū)信息:"?+?bookName[number-1]); } else { try? { System.out.println("沒(méi)有該序號(hào)圖書(shū)!"); select(); }?catch?(Exception?e)? { e.printStackTrace(); } } } else { try? { System.out.println("輸入錯(cuò)誤,請(qǐng)輸入數(shù)字!"); bookNumber(); }?catch?(Exception?e)? { e.printStackTrace(); } } } public?void?inName() { System.out.println("輸入圖書(shū)名稱:"); @SuppressWarnings("resource") String?name?=??new?Scanner(System.in).nextLine(); boolean?flag?=?Arrays.asList(bookName).contains(name); if(flag) { System.out.println("圖書(shū)信息:"?+?name); } else try? { System.out.println("沒(méi)有該名稱圖書(shū)!"); select(); }?catch?(Exception?e1)? { e1.printStackTrace(); } } public?void?select() { System.out.println("輸入命令:1-按書(shū)名查找圖書(shū);?2-按序號(hào)查找圖書(shū)"); @SuppressWarnings("resource") Scanner?inCammond?=?new?Scanner(System.in); try { if(inCammond.hasNextInt())? { int?cammond?=?inCammond.nextInt(); if(cammond?==?1) { inName(); } else?if?(cammond?==?2) { bookNumber(); } else { throw?new?CammondException(); } } else { throw?new?CammondException(); } }catch?(CammondException?e)? { System.out.println("命令無(wú)效,請(qǐng)按提示輸入命令?。。?); select(); } } }
//CammondException時(shí)一般的自定義類型
2019-04-23
我其實(shí)都不懂什么是異常