java入门第三季第一章习题小解(希望高手给与改进!)

import java.util.Scanner;
public class SeachBookMachine {
    static void method1(String xuhao[],String  shuming[]){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入图书名称:");
        try {
            String booksm=input.next();
            String bool="null";
            for(int i=0;i<shuming.length;i++){
                if(shuming[i].equals(booksm)){
                System.out.println(xuhao[i]+":"+shuming[i]);
                bool="not null";
                }
            }
            if(bool.equals("null")){
                throw new NoExistException("圖書不存在!");
            }
        } catch (NoExistException e) {
//          System.out.println(e);
            e.toshow();
            method1(xuhao, shuming);
        }
    }

static void method2(String xuhao[],String  shuming[]){
    try {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入图书序號:");
        String bookxh=input.nextLine();
        String bool="null";
        for(int i=0;i<xuhao.length;i++){
            if(xuhao[i].equals(bookxh)){
            System.out.println(xuhao[i]+":"+shuming[i]);
            bool="not null";
            }
          }
        if(bool=="null")
        throw new SlopOverException("圖書不存在~");
    } catch (SlopOverException e) {
//      System.out.println(e);
        e.toshow();
        method2(xuhao, shuming);
    }
    }
     public static void main(String []args) {
         String xuhao[]={"A1","B1","B2","B3","C1","D1"};
         String shuming[]={"語文","數學","數學習題","數學試卷","英語","音樂"};
         boolean choice=true;
         Scanner input=new Scanner(System.in);
         while(choice){
             try {
                System.out.println("輸入命令:1-按照書名查找圖書  2-按照序號查找圖書");
                int choi=input.nextInt();
                if(choi>2||choi<1)
                    throw new NumberFormatException("请输入数字1或2!");
                switch (choi) {
                case 1:
                    method1(xuhao, shuming);
                    break;
                case 2:
                    method2(xuhao, shuming);
                    break;
                    }
                choice=false;
                System.out.println("你還想繼續查詢碼?(true or false)");
                choice=input.nextBoolean();
            } 
             catch (NumberFormatException e) {
                System.out.println(e);
                input.nextLine();
                continue;
            }catch (Exception e) {
                System.out.println(e+":输入命令错误,请按照提示输入!");
                input.nextLine();
                continue;
            }
         }
    }
}
class NoExistException extends Exception{
    String string;
     NoExistException(String stri){
        string=stri;
    }
    void toshow(){
        System.out.println(string+"请检查书名是否正确...");
    }
}
class SlopOverException extends Exception{
    String string;
    SlopOverException(String stri){
        string=stri;
    }
    void toshow(){
        System.out.println(string+"请检查序号是否正确...");
    }
}
点赞