import java.util.Scanner;
public class BookGeneral2 {
public static void main(String[] args) {
String[] ShuMingArray = {"数学建模","工程数学","Java入门","C语言","数据库","网页设计"};
String[] XuHaoArray = {"001","002","003","004","005","006"};
Scanner sc = new Scanner(System.in);
boolean start = true;
while(start){
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
try{
String number1 = sc.next(); //1或2
switch(number1){
case "1" : BookName(ShuMingArray,XuHaoArray);start=false;break;
case "2" : Number(ShuMingArray,XuHaoArray);start=false;break;
default : throw new SelectException("输入命令错误!请根据提示输入数字命令!");
}
}catch(SelectException e){
System.out.println(e.getMessage());
continue;
}
}
}
/*
* 自定义异常:
*/
//书名异常
public static void BookName(String ShuMingArray[],String XuHaoArray[]){
Scanner ac = new Scanner(System.in);
String Buff = "Blue";
boolean start = true;
while(start){
System.out.println("输入图书名称:");
try{
String shuming = ac.next(); //书名
for(int i=0;i<ShuMingArray.length;i++){
if(ShuMingArray[i].equals(shuming)){
System.out.println("图书信息:《"+ShuMingArray[i]+"》,作者:XXX"+",序列号:"+XuHaoArray[i]+" -查询结束");
Buff = "Red";
start=false; //结束查询
}
}
if(Buff.equals("Blue")){
throw new BookNameException("没有找到该图书!#1");
}
}catch(BookNameException e){
System.out.println(e.getMessage());
continue;
}
}
}
//序号异常
public static void Number(String ShuMingArray[],String XuHaoArray[]){
Scanner bc = new Scanner(System.in);
String Buff = "Blue";
boolean start = true;
while(start){
System.out.println("输入图书序号:");
try{
String xuhao = bc.next(); //序号
for(int j=0;j<XuHaoArray.length;j++){
if(XuHaoArray[j].equals(xuhao)){
System.out.println("图书信息:《"+ShuMingArray[j]+"》,作者:XXX"+",序列号:"+XuHaoArray[j]+" -查询结束");
Buff = "Red";
start=false; //结束查询
}
}
if(Buff.equals("Blue")){
throw new NumberException("没有找到该图书!#2");
}
}catch(NumberException e){
System.out.println(e.getMessage());
continue;
}
}
}
}
打开App,阅读手记
热门评论
找了很多关于“异常”资料和“异常”视屏讲解的学习,花了4-5天才完成这个鬼东东(总共用了5-7个小时学习异常,4-6小时编码TT)
找了很多关于“异常”资料和“异常”视屏讲解的学习,花了4-5天才完成这个鬼东东(总共用了5-7个小时学习异常,4-6小时编码TT)