package?jieshuxitong;
import?java.util.Scanner;
import?java.util.InputMismatchException;
public?class?Jieshu?{public?static?void?main(String[]?args)?{
Jieshu?js?=?new?Jieshu();
js.runtime();
}
public?class?NoBookException?extends?Exception{public?NoBookException()?{
}
public?NoBookException(String?message)?{
super(message);
}
}
public?class?SrcwException?extends?Exception{
public?SrcwException(String?message)?{
super(message);
}
}
public?void?runtime()?{
String[]?books?=?{"人間失格","我是貓","月亮和六便士","格物致知"};
Scanner?sc?=?new?Scanner(System.in);
System.out.println("******歡迎使用圖書租賃系統(tǒng)******");
System.out.println("1、按照名稱查找圖書;?2、按照序號查找圖書");
try?{
int?sr?=?sc.nextInt();
if(?sr?==?1?)?{
System.out.println("請輸入書的名稱:");
String?name?=?sc.next();
int?bl?=?0;
for(int?i?=?0;i<books.length;i++)?{
if(books[i].equals(name))?{
bl=?i;
}else?{
continue;
}
}
if(books[bl].equals(name))?{
System.out.println("書來啦~~~"+books[bl]);
}else?{
throw?new?NoBookException();
}
}else?if(?sr?==?2?){
System.out.println("請輸入書的序號:");
int?num?=?sc.nextInt();
if(num>0?&&?num<=books.length)?{
System.out.println("書來啦~~~:"+books[num-1]);
}else?{
throw?new?NoBookException("輸入錯誤,重輸:");
}
}else?{throw?new?SrcwException("輸入錯誤,請重新輸入:");
}
}catch(NoBookException?e){System.out.println("查無此書,重輸:");
runtime();
}catch(SrcwException?e)?{System.out.println(e.getMessage());
runtime();
}catch(InputMismatchException?e)?{System.out.println("請按提示重新輸入!");
runtime();
}
}
}
2019-08-17