继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

了不起的水獭
关注TA
已关注
手记 2
粉丝 0
获赞 2
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("图书不存在!");

        }
    }
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP