一共4个class,俩个异常类 一个功能类 一个main类
1.存在异常
package com.imooc.ruige;
public class ExistedException extends Exception {
public ExistedException(String Message) {
// TODO Auto-generated constructor stub
super(Message);
}
public ExistedException() {
System.out.println("图书不存在!");
}
}
2.类型异常
package com.imooc.ruige;
public class TypeException extends Exception {
public TypeException(String Message) {
super(Message);
}
public TypeException() {
System.out.println("命令输入有误!请根据提示输入数字命令!");
}
}
3.功能类
package com.imooc.ruige;
import java.util.Scanner;
public class Book {
String[] book ={"数据结构","Effective c","通信原理","信号与系统","高数"};
Scanner in =new Scanner(System.in);
int isFind;
int orderNum;
public void brobook () throws TypeException, ExistedException{
isFind=0;
System.out.println("输入命令:1-按照名称查找图书:2-按照序号查找图书");
try{ orderNum=Integer.parseInt(in.next());}
catch(Exception e){
throw new TypeException();
}
if(orderNum==1){
System.out.println("输入图书名称:");
String name= in.next();
for(String bookname:book){
if(bookname.equals(name)){
isFind=1;
System.out.println("book:"+bookname);}
}
if(isFind==0)
throw new ExistedException();
}
else if(orderNum==2){
System.out.println("输入图书序号");
int bookNum = in.nextInt();
if(0<=bookNum && bookNum<=book.length)
System.out.println("book:"+book[bookNum]);
else
throw new ExistedException();
}
}
}
4.主类(main)
package com.imooc.ruige;
import java.util.Scanner;
public class BroBook {
public static void main(String[] args) {
// TODO Auto-generated method stub
Book book =new Book();
while(true){
try {
book.brobook();
} catch (TypeException e) {
// TODO Auto-generated catch block
}
catch(ExistedException e){
}
catch(Exception e){
}
}
}
}
运行效果如图
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦