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

第3季异常处理机制课后题代码

慕神3859940
关注TA
已关注
手记 3
粉丝 1
获赞 16

//类
package com.book;
import java.util.*;
public class BookSystem
{
private String bookName;
private int bookId;

public BookSystem(int bookId, String bookName) throws Exception
{   

    this.bookId=bookId;
    this.bookName=bookName; 
}

public String getBookName()
{
    return bookName;
}

public int getBookId()
{
    return bookId;
}

//main 函数
public static void main(String[] args)
{

 try
{
    BookSystem [] Lib={new BookSystem(1, "Acient Chinese"),
             new BookSystem(2, "Math"),
             new BookSystem(3, "English"),
             new BookSystem(4, "Politics"),
             new BookSystem(5, "History"),
             new BookSystem(6, "Music"),
             new BookSystem(7, "Physics")};
    System.out.println("1-find your book using bookId; 2-find your book using bookName");
    Scanner input0=new Scanner(System.in);
     String option=input0.next();
     if(option.equals("1"))
     {
         System.out.println("Please input the bookId");
         Scanner input=new Scanner(System.in);
         int id=input.nextInt();
         for(int i=0;i<Lib.length-1;i++)
         {
             if(id==Lib[i].getBookId())
             {
                 System.out.println("book:"+Lib[i].bookName);
             }
         } 

           {
              Exception e=new Exception();
              throw new Exception("The book does not exist!");
           }
     }
     else if(option.equals("2"))

     {   System.out.println("Please input the bookName");
         Scanner scanner=new Scanner(System.in);
         String name=scanner.next();
         for(int i=0;i<Lib.length-1;i++)
         {
             if(name.equals(Lib[i].getBookName()))
             {
                 System.out.println("book:"+Lib[i].bookName);
             }
         } 

            {
                Exception e=new Exception();
                throw new Exception("This book does not exist");
            }

     }
     else
             {
                throw new Exception("Please input number as indicated!!");
             }

} catch (Exception e)
{

    //e.printStackTrace();
    System.out.println(e.getMessage());

}

}

}

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