package?com.study2;
import?java.util.Scanner;
//異常處理
//所有異常類都繼承于Throwable
//兩大類,Exception類和Error類
//Error:?VirtualMachineError,ThreadDeath,虛擬機(jī)錯(cuò)誤,線程鎖死
//Exception:非檢查異常RuntimeException(運(yùn)行時(shí)異常)
//檢查異常:IOException,SQLException
////RuntimeException:
//空指針異常(NullPointerException),數(shù)組越界異常(ArrayIndexOutOfBoundsException)
//類型轉(zhuǎn)換異常(ClassCastException),算術(shù)異常(ArithmeticException)
//InputMismatchException輸入不匹配異常
//ArithmeticException算術(shù)異常
//Exception類中的printStackTrace();堆棧跟蹤信息
//InitCause方法,追溯異常的原因,對(duì)異常進(jìn)行包裝
//自定義異常
//nextLine可以讀到空格,next不行,兩者遇見(jiàn)回車結(jié)束
public?class?Initail?{
????public?static?void?main(String[]?args)?{
????????choose();
????}
????//書架
????private?static?Scanner?input?=?new?Scanner(System.in);
????private?static?Book[]?books?=?new?Book[]{
????????????new?Book("龍族",?"001"),
????????????new?Book("三體",?"002"),
????????????new?Book("毛選",?"003"),
????};
????//選書
????private?static?void?choose()?{
????????System.out.println("輸入命令:1?按照書名查找圖書??2?按照ISBN號(hào)查找圖書");
????????try?{
????????????int?choice?=?input.nextInt();
????????????if?(choice?==?1)?SearchByName();
????????????if?(choice?==?2)?SearchByNumber();
????????????else{
????????????????System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入");
????????????????choose();
????????????}
????????}?catch?(NameException?e)?{
????????????e.printStackTrace();
????????????choose();
????????}catch?(NumberException?e){
????????????e.printStackTrace();
????????????choose();
????????}catch?(Exception?e){
????????????System.out.println("輸入命令有誤");
????????????choose();
????????}
????}
????//按照書名選書
????private?static?void?SearchByName()?throws?NameException?{//拋出聲明
????????System.out.println("輸入要查找的書籍名稱:");
????????String?inputname="0";
????????try?{
????????????inputname?=?input.next();
????????}?catch?(Exception?e)?{//異常時(shí)的處理
????????????System.out.println("輸入不合法");
????????????SearchByName();//重新進(jìn)入該方法
????????}
????????boolean?check?=?false;
????????//檢查輸入是否合法
????????//equals函數(shù):字符串比較,若相等則返回true,否則返回false
????????for?(int?i?=?0;?i?<?books.length;?i++)?{
????????????if?(books[i].getBookname().equals(inputname))?{
????????????????System.out.println("書籍名稱:"?+?inputname?+?"???"?+?"ISBN:"?+?books[i].getISBN());
????????????????check?=?true;
????????????}
????????}
????????if?(check?==?false)?throw?new?NameException();
????}
????//按照IBSN號(hào)選書
????private?static?void?SearchByNumber()?throws?NumberException?{//拋出聲明
????????String?inputnumber?="0";
????????System.out.println("輸入要查找的書籍ISBN:");
????????try?{
????????????inputnumber?=?input.next();
????????}?catch?(Exception?e)?{//異常時(shí)的處理
????????????System.out.println("輸入不合法");
????????????SearchByNumber();
????????}
????????boolean?check?=?false;
????????//檢查輸入是否合法
????????for?(int?i?=?0;?i?<?books.length;?i++)?{
????????????if?(books[i].getISBN().equals(inputnumber))?{
????????????????System.out.println("書籍名稱:"?+?books[i].getBookname()?+?"???"?+?"ISBN:"?+?inputnumber);
????????????????check?=?true;
????????????}
????????}
????????if?(check?==?false)?throw?new?NumberException();
????}
}
//自定義異常
package?com.study2;
public?class?NameException?extends?Exception?{
????//構(gòu)造方法
????NameException(){
????????super("該圖書不存在!");
????}
}
package?com.study2;
public?class?NumberException?extends?Exception?{
????NumberException(){
????????super("該圖書不存在!");
????}
}
package?com.study2;
public?class?Book?{
????private?String?bookname;
????private?String?ISBN;//?International?Standard?Book?Number
????public?String?getBookname()?{
????????return?bookname;
????}
????public?void?setBookname(String?bookname)?{
????????this.bookname?=?bookname;
????}
????public?String?getISBN()?{
????????return?ISBN;
????}
????public?void?setISBN(String?ISBN)?{
????????this.ISBN?=?ISBN;
????}
????//有參的構(gòu)造函數(shù)
????public?Book(String?bookname,String?ISBN){
????????this.bookname=bookname;
????????this.ISBN=ISBN;
????}
}
2020-03-20
闊以的,很強(qiáng),也不知道要回答什么,方法封裝的很好,可以改成public去使用
2020-04-02
書架那一欄創(chuàng)建得是對(duì)象數(shù)組嗎?? 為什么我復(fù)制到編輯器全報(bào)錯(cuò)i
2020-03-26
問(wèn)下,為什么我打出來(lái)不存在的書名,不會(huì)輸出? 該圖書不存在? 看著你的代碼打的
2020-03-25
厲害,受教了?
2020-03-24
看你寫的代碼真的非常好。我是初學(xué)者,想請(qǐng)教一個(gè)類似這樣代碼的問(wèn)題。第一步程序要輸入一個(gè)規(guī)定的數(shù)字,但是輸入字母的話就會(huì)拋出異常,我想讓它能夠返回方法并重新輸入,但是按照這個(gè)方式寫,結(jié)果是不停地返回該方法的輸出部分,沒(méi)有再重新輸入的機(jī)會(huì)。我的理解是輸入這個(gè)字母后,程序會(huì)返回到方法中,但是這個(gè)輸入值依然是這個(gè)字母,就無(wú)限循環(huán)報(bào)錯(cuò)了,不知道我想的對(duì)不對(duì),請(qǐng)問(wèn)這個(gè)怎么解決呢