亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

程序代碼。

package?com.immc.exception;
/**
?*?圖書信息儲存
?*?書名?bookName
?*?圖書序號?bookNumber
?*?@author?Administrator
?*
?*/
public?class?Book?{
	private?String?bookName;
	private?int?bookNumber;
	public?Book()?{
		super();
		//?TODO?Auto-generated?constructor?stub
	}
	public?Book(String?bookName,?int?bookNumber)?{
		super();
		this.bookName?=?bookName;
		this.bookNumber?=?bookNumber;
	}
	public?String?getBookName()?{
		return?bookName;
	}
	public?void?setBookName(String?bookName)?{
		this.bookName?=?bookName;
	}
	public?int?getBookNumber()?{
		return?bookNumber;
	}
	public?void?setBookNumber(int?bookNumber)?{
		this.bookNumber?=?bookNumber;
	}
	
}

package?com.immc.exception;
/*
?*?自定義異常:
?*?		圖書不存在異常
?*/
?
public?class?bookNotExistException?extends?Exception{

	public?bookNotExistException()?{
		super();
		System.out.println("圖書不存在!");
		//?TODO?Auto-generated?constructor?stub
	}
}

package?com.immc.exception;
/*
?*?自定義異常:
?*?		命令序號不在范圍內(只能為1或2)
?*/
?
public?class?demandErrorException?extends?Exception{

	public?demandErrorException()?{
		super();
		System.out.println("命令序號不在范圍內!");
		//?TODO?Auto-generated?constructor?stub
	}	
}

package?com.immc.exception;
import?java.util.Scanner;

public?class?userInput?{
	public?int?inputDemand(){
		System.out.println("輸入命令:1-按名稱查找圖書;2-按序號查找圖書");
		Scanner?sc?=?new?Scanner(System.in);
		return?sc.nextInt();	?
	}
	
	public?String?inputBookName(){
		System.out.println("請輸入圖書名稱:");
		Scanner?sc?=?new?Scanner(System.in);
		return?sc.nextLine();	?
	}
	
	public?int?inputBookNumber(){
		System.out.println("請輸入圖書序號:");
		Scanner?sc?=?new?Scanner(System.in);
		return?sc.nextInt();
	}
}

package?com.immc.exception;

import?java.util.InputMismatchException;

public?class?ExceptionTest?{
	
	userInput?us?=?new?userInput();
	public?void?searchBook(){
		while(true){
			int?demand?=?0;
			try{
				demand?=?us.inputDemand();
				demandError(demand);
			}catch(InputMismatchException?e){
				System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");
				continue;
			}catch(demandErrorException?e){
				continue;
			}
			
			switch(demand){
				case?1:
					String?str?=?us.inputBookName();
					try{
						showBook(searchByName(str));
						break;
					}catch(bookNotExistException?e){
						continue;
					}
				case?2:
					int?num?=?us.inputBookNumber();
					try{
						showBook(searchByNumber(num));
						break;
					}catch(bookNotExistException?e){
						continue;
					}
			}
		}
		
	}
	
	private?Book?searchByName(String?name)?throws?bookNotExistException{
		//for?(循環變量類型?循環變量名稱?:?要被遍歷的對象)?循環體
		for(Book?book?:?BookIinitial.book){
			if(book.getBookName().equals(name)){
				return?book;
			}
		}
		
		throw?new?bookNotExistException();
	}
	
	private?Book?searchByNumber(int?number)?throws?bookNotExistException{
		if(number>BookIinitial.book.length?||?number<1){
			throw?new?bookNotExistException();
		}else{
			for(Book?book?:?BookIinitial.book){
				if(book.getBookNumber()?==?number){
					return?book;
				}
			}
			return?null;
		}
	}
		
	private?int?demandError(int?demand)?throws?demandErrorException{
		if(demand?==?1||demand?==?2){
			return?demand;
		}else{
			throw?new?demandErrorException();
		}
	}
	
	private?void?showBook(Book?book){
		System.out.println("圖書序號:"+book.getBookNumber()+
				"??書名:"+book.getBookName());
	}

}
package?com.immc.exception;

public?class?BookIinitial?{
	static?Book[]?book?=?{new?Book("高等數學",1),new?Book("數據結構",2),
		new?Book("中國語文",3),new?Book("大學英語",4)};
}
package?com.immc.exception;

public?class?Test?{

	/**
	?*?@param?args
	?*/
	public?static?void?main(String[]?args)?{
		//?TODO?Auto-generated?method?stub
		ExceptionTest?ex?=?new?ExceptionTest();
		ex.searchBook();
	}

}


正在回答

0 回答

舉報

0/150
提交
取消
Java入門第三季
  • 參與學習       409775    人
  • 解答問題       4546    個

Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧

進入課程

程序代碼。

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號