ITPub博客

首页 > IT职业 > IT职场 > java第三季异常作业:借书系统

java第三季异常作业:借书系统

IT职场 作者:piny 时间:2021-09-09 09:00:21 0 删除 编辑
package com.imooc;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Initial {

    static Scanner input=new Scanner(System.in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Initial xx= new Initial();

breakWhile:while(true){
            System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书。");
            try{
            switch(input.next()){
            case "1":
                System.out.println("请输入图书名称:");
                try{
                    String c=input.next();
                    switch(c){
                    case "语文":
                        System.out.println("book:语文");
                        break breakWhile;
                    case "数学":
                        System.out.println("book:数学");
                        break breakWhile;
                    case "英语":
                        System.out.println("book:英语");
                        break breakWhile;
                    default :
                        System.out.println("没有名字是:"+c+"的书籍!");
                        continue;
                    }
                }catch(Exception e){
                    Exception zy=new Exception("请输入字符串!");
                    zy.initCause(e);
                    throw zy;
                }
            case "2":
                System.out.println("请输入图书序号:");
                try{
                    String d=input.next();
                    switch(d){
                    case "1":
                        System.out.println("语文");
                        break breakWhile;
                    case "2":
                        System.out.println("数学");
                        break breakWhile;
                    case "3":
                        System.out.println("英语");
                        break breakWhile;
                    default :
                        System.out.println("没有序号是"+d+"的书籍!");
                        continue;
                        }
                    }catch(Exception e){
                        Exception zy=new Exception("请输入整数!");
                        zy.initCause(e);
                        throw zy;
                    }
            default :
                System.out.println("输入不正确!");
                break;
            }
        }catch(Exception e){
            System.out.println("不知名异常~");
            }
        }
    }
}

有一个问题就是:目前可以显示的异常几乎都是通过switch语句的default来抛出,catch块从未执行过...这个问题该怎么解决呢?还是不用解决这样就行了呢?望大神给我解答#。#图片描述图片描述

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3034/viewspace-2798522/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-05-13

  • 博文量
    137
  • 访问量
    8806