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