public?class?BorrowBooks?{
????Book[]?books?=?{new?Book("紅樓夢",1),new?Book("高數(shù)",2)};
????public?static?void?main(String[]?args)?{
????????System.out.println("請輸入書名及圖書序號來查找圖書");
????????boolean?flag?=?true;
????????while?(flag)?{
????????????try?{
????????????????????System.out.println("請輸入圖書序號");
????????????????????Scanner?scanner?=?new?Scanner(System.in);
????????????????????int?number?=?scanner.nextInt();
????????????????????if?(number?!=?1?&&?number?!=?2)?{
????????????????????????throw?new?ErrorCommandException();
????????????????????}
????????????????????System.out.println("請輸入圖書名字");
????????????????????String?bookName?=?scanner.next();
????????????????????BorrowBooks?books?=?new?BorrowBooks();
????????????????????if?(!books.books[number-1].name.equals(bookName))?{
????????????????????????throw?new?BookNotExistException();
????????????????????}?else?{
????????????????????????books.books[number-1].showBookName();
????????????????????????flag?=?false;
????????????????????}
????????????}?catch?(ErrorCommandException?e)?{
????????????????e.printStackTrace();
????????????????System.out.println("請重新輸入序號");
????????????}?catch?(BookNotExistException?e)?{
????????????????e.printStackTrace();
????????????????System.out.println("請重新輸入書名");
????????????}?catch?(InputMismatchException?e){
????????????????e.printStackTrace();
????????????????System.out.println("請輸入數(shù)字");
????????????}
????????}
????}
}
public?class?ErrorCommandException?extends?Exception{
????public?ErrorCommandException(){
????????super("命令輸入錯誤,請按照提示輸入數(shù)字");
????}}
public?class?BookNotExistException?extends?Exception{
????public?BookNotExistException(){
????????super("圖書不存在");
????}
}
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);
????}
}
2022-01-21
你這個不是按題目做的啊? 題目是要求? 根據(jù)書名查詢或者序號查詢? 你這個是序號和書名都要輸入