package?borrow_books;
import?java.util.Scanner;
import?java.util.InputMismatchException;
public?class?Test?{
????int?command;
????String?inName;
????int?inNum;?
????Scanner?input?=?new?Scanner(System.in);
????Book[]?books?=?{new?Book("數(shù)據(jù)結構",?1),?new?Book("高數(shù)",?2)};
????
????public?static?void?main(String[]?args)?{
????????while(true)?{
????????????Test?t?=?new?Test();
????????????t.inputCommand();
????????????if(t.command?==?1)?{
????????????????t.inputName();
????????????}else?if(t.command?==?2){
????????????????t.inputNumber();
????????????}
????????}
????}
????public?void?inputCommand()?{
????????try?{
????????????System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");
????????????command?=?input.nextInt();
????????????if((command?!=?1)?&&?(command?!=?2))?{
????????????????throw?new?ErrorCommandException();
????????????}
????????}catch(InputMismatchException?ie)?{
????????????ErrorCommandException?ee?=?new?ErrorCommandException();
????????????System.out.println(ee.getMessage());
????????}catch(ErrorCommandException?ee)?{
????????????System.out.println(ee.getMessage());
????????}
????}
????public?void?inputName()?{
????????try?{
????????????System.out.println("輸入圖書名稱");
????????????inName?=?input.next();
????????????for(int?i?=?0;?i?<?books.length;?i++)?{
????????????????if(inName.equals(books[i].name))?{
????????????? ????books[i].showBookName();
????????????? ????return;
????????????? }
????????????}
????????????throw?new?BooknotExistException();
????????}catch(Exception?e)?{
????????????System.out.println(e.getMessage());
????????}
????}
????public?void?inputNumber()?{
????????while(true)?{
????????????try?{
????????????????System.out.println("輸入圖書序號");
????????????????inNum?=?input.nextInt();
????????????????for(int?i?=?0;?i?<?books.length;?i++)?{
????????????????????if(inNum?==?books[i].number)?{
????????????????????????books[i].showBookName();
????????????????????????return;
????????????????????}
????????????????}
????????????????throw?new?BooknotExistException();
????????????}catch(InputMismatchException?ie)?{
????????????????input.nextLine();
????????????????ErrorCommandException?ee?=?new?ErrorCommandException();
????????????????System.out.println(ee.getMessage());
????????????}catch(BooknotExistException?be)?{
????????????????System.out.println(be.getMessage());
????????????????return;
????????????}
????????}
????}
}
package?borrow_books;
public?class?ErrorCommandException?extends?Exception?{
????public?ErrorCommandException()?{
????????super("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");
????}
}
package?borrow_books;
public?class?BooknotExistException?extends?Exception?{
????public?BooknotExistException()?{
????????super("圖書不存在!");
????}
}
package?borrow_books;
public?class?Book?{
????String?name;
????int?number;
????
????public?Book(String?name,?int?number)?{
????????this.name?=?name;
????????this.number?=?number;
????}
????
????public?void?showBookName()?{
????????System.out.println("book:"+name);
????}
}
2020-08-27
感覺上就只有你編寫的代碼是方便的,我挺喜歡的。
2021-08-15
你這有一個bug,輸入1至3選擇時,如果輸入字母會報錯的
2021-03-13
謝謝分享,學習了~
2020-09-15
思路清晰,代碼簡潔,已參考,謝謝樓主。