问答详情
源自:1-9 经验总结

交作业。。。。

public class BorrowBooks {
    Book[] books = {new Book("红楼梦",1),new Book("高数",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("请输入数字");
            }

        }

    }
}


public class ErrorCommandException extends Exception{
    public ErrorCommandException(){
        super("命令输入错误,请按照提示输入数字");

    }}



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);
    }
}


提问者:weixin_慕桂英9495175 2021-12-27 14:02

个回答

  • 心形罐装病毒
    2022-01-21 23:29:54

    你这个不是按题目做的啊  题目是要求  根据书名查询或者序号查询  你这个是序号和书名都要输入