手记

借鉴的查询图书的异常捕获 怎么用类写啊 这样写的感觉很不友好

package tryCatch;

import java.util.Scanner;

public class LiberaryTest {
private static final String[] Book ={"1:线性代数","2:高数","3:java","4:论语"};

public static void main(String[] args) {
    System.out.println("欢迎来到图书馆,请查询你所要的图书");        
    LiberaryTest lt = new LiberaryTest();
    lt.test();

}

public void test(){
    System.out.println("输入命令:1-按序号查询    2-按书名查询");
    try{
        Scanner input = new Scanner(System.in);
        int result = input.nextInt();
        switch(result){
        case 1:
            Num();
            break;
        case 2:
            Name();
            break;
        default:
            System.out.println("你的输入有错!");          
        }

    }catch(Exception e){
        e.printStackTrace();
        System.out.println("请输入正确数字选择类别查询");
    }
    test();
}

public void Num(){
    System.out.println("请输入你想检索的序号:");
    try{
        Scanner input2 = new Scanner(System.in);
        int result2 = input2.nextInt();
        if(result2>Book.lengthresult2<1){
            System.out.println("图书不存在");
        }else{
            System.out.println("你的书:"+Book[result2-1]);
        }                       
    }catch(Exception e){
        e.printStackTrace();
        System.out.println("请输入数字");
    }
    test();
}
public void Name(){
    System.out.println("请输入你想检索的书名:");
    try{
        Scanner input3 = new Scanner(System.in);
        String result3 = input3.next();
        switch(result3){
            case "线性代数":
                System.out.println("你的图书:"+Book[0]);
                break;
            case "高数":
                System.out.println("你的图书:"+Book[1]);
                break;
            case "java":
                System.out.println("你的图书:"+Book[2]);
                break;
            case "论语":
                System.out.println("你的图书:"+Book[3]);
                break;
            default:
                System.out.println("图书不存在");
        }       
    }catch(Exception e){
        e.printStackTrace();
        System.out.println("你的输入有误");
    }
    test();
}

}

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