终于做完了,大家帮忙看看有哪里还需要优化完善的
//这是测试类部分,自定义异常类大家都一样,就不贴了:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t=new Test();
t.borrow();
}
public void borrow(){
String[] books={"西游记","三国演义","水浒传","红楼梦"};
Scanner input=new Scanner(System.in);
System.out.println("*******欢迎使用图书馆借书系统*******");
System.out.println("1. 按照书名查找图书 2.按照序号查找图书");
try{
int num=input.nextInt();
if(num==1){
System.out.println("请输入书名:");
String name=input.next();
for(int i=0;i<books.length;i++){
if(books[i].equals(name)){
System.out.println("找到图书---"+books[i]);
}else{
throw new NobookException("图书不存在,请重新输入");
}
}
}else if(num==2){
System.out.println("请输入序号:");
int no=input.nextInt();
if(no>0&&no<=books.length){
System.out.println("找到图书---"+books[no-1]);
}else{
throw new NobookException("图书不存在,请重新输入");
}
}else{
throw new ErrorCodeException("命令错误,请重新输入");
}
}catch(NobookException e){
System.out.println(e.getMessage());
borrow();
}catch(ErrorCodeException e){
System.out.println(e.getMessage());
borrow();
}catch(InputMismatchException e){
System.out.println("请按提示重新输入!");
borrow();
}
}
}