請大神多多指教,有哪里可以更加完善
package bookStroe;
import java.util.Scanner;
public class BS {
???????? public static void main(String[] args){
???????? String[][] book = {{"1","論語"},{"2","數(shù)據(jù)庫"},{"3","建模"}}; ? ?//初始化書店的書
???????? while(true) { ? ?//無限循環(huán)查書
???????????????? try {
???????????????????? System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書;");
???????????????????? int num=new Scanner(System.in).nextInt();
???????????????????? if(num!=1&&num!=2) {? ? ? ?//只有1,2兩個選項,輸入其他命令均拋出異常
???????????????????????? throw new ClassCastException("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");?
???????????????????? }
???????????????????? String bName=null;????????//定義一個空指針
???????????????????? if(num==1) {
? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println("圖書名稱:");
? ? ? ? ? ? ? ? ? ? ? ? ?String name=new Scanner(System.in).nextLine();
???????????????????????? for(int i=0;i<book.length;i++) {
???????????????????????? ???? if(name.equals(book[i][1])) {
???????????????????????????? ???? bName="book:"+book[i][1];????????//有找到書就賦值給先前定義的空指針
???????????????????????????? ???? break;????????//停止查書系統(tǒng)
???????????????????????? ???? }
???????????????????? ???? }
???????????????????? System.out.println(bName.toString());????????//引用,如果bName為空則拋出異常
???????????????????? break;????????//退出查書系統(tǒng)
???????????????? }
???????????????? if(num==2) {
???????????????????? System.out.println("圖書序號:");
???????????????????? int mark=new Scanner(System.in).nextInt();
???????????????????? System.out.println("book:"+book[mark-1][1]);
???????????????????? break;????????//退出查書系統(tǒng)
???????????????? }
???????????? }catch(NullPointerException e) {
???????????????? System.out.println("圖書不存在");????????????//空指針異常
???????????? }catch(ArrayIndexOutOfBoundsException e) {
???????????????? System.out.println("沒那么多本書");????????//數(shù)組溢出異常
???????????? }catch(Exception e) {
???????????????? System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");????????//其他異常
???????????? }
???????? }
???? }
}
2019-10-09
可以考慮加一個選擇圖書后重新回到選課系統(tǒng),在輸入時根據(jù)輸入的值判斷是否退出查看操作,,,另外可以建議你向多本書查詢
2019-10-09
強強強