为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Java

//类
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());

}

}

}

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消