手记

图书馆借书系统(异常抛出)

public class Book {
    public String bookname;
    public int booknum;
    public  Book(String bookname,int booknum){
        this.bookname=bookname;
        this.booknum=booknum;
    }
}
public class NoexistException extends Exception {
    public NoexistException(){

    };
    public NoexistException(String message){
        super(message);
    };
}
import java.util.Scanner;
public class brrowbook {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Book book[]={new Book("高数",1),
                     new Book("数据结构",2),

        };
        while(true){
            System.out.println("请输入数字命令:1-按照书名查找;2-按照序号查找");
            Scanner sn=new Scanner(System.in);
            try{
                int input_in=sn.nextInt();
                if(input_in==2){
                    System.out.println("请输入图书序号");
                    int input_in_num=sn.nextInt();
                    boolean num=false;
                    for(int i=0;i<book.length;i++){
                        if(input_in_num==book[i].booknum){
                            System.out.println("book:"+book[i].booknum);
                            num=true;
                        }
                    }
                    if(!num){
                        warn();
                    }
                }else if(input_in==1){
                    System.out.println("请输入图书名称:");
                    String input_in_name=sn.next();
                    boolean name=false;
                    for (int i=0;i<book.length;i++){
                        if(input_in_name.equals(book[i].bookname)){
                            System.out.println("book"+book[i].bookname);
                            name=true;
                        }
                    }
                    if(!name){
                        warn();
                    }
                }
                    else{
                        System.out.println("命令输入错误,请重新输入!");
                    }

                }catch(NoexistException e){
                    System.out.println("图书不存在");

                }catch(Exception e){
                    System.out.println("命令输入错误,请输入数字命令");
                }

            }
            }
            public static void warn()throws NoexistException{
                throw new NoexistException("图书不存在!");

        }
    }
0人推荐
随时随地看视频
慕课网APP