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

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

交作業---模擬結束系統

public?class?BorrowBook?{
????public?static?String[]?books?=?{"第一行代碼",?"thinking?in?java",?"安卓探索藝術"};
????public?static?Scanner?sc;
????public?static?boolean?flag?=?true;
????public?static?boolean?isIndexOrName?=?false;

????public?static?void?main(String[]?args)?{
????????System.out.println("***歡迎來到模擬圖書系統***");
????????System.out.println("請輸入您要查找圖書的方式:1.按圖書序號查找"?+?"\t"?+?"2.按圖書名稱查找");
????????System.out.println("請輸入一個有效查找方式:");
????????find2IndexOrName();
????}

????private?static?void?find2IndexOrName()?{
????????sc?=?new?Scanner(System.in);
????????int?input?=?sc.nextInt();
????????try?{
????????????if?(1?==?input)?{
????????????????System.out.println("請輸入有效圖書序號:");
????????????????int?index?=?sc.nextInt();
????????????????find2Index(index);
????????????}?else?if?(2?==?input)?{
????????????????System.out.println("請輸入正確的書名:");
????????????????String?name?=?sc.next();
????????????????find2Name(name);
????????????}?else?{
????????????????throw?new?Exception();
????????????}
????????}?catch?(Exception?e)?{
????????????System.out.println("請輸入正確指令:");
????????????find2IndexOrName();
????????}
????}

????public?static?void?find2Name(String?name)?{
????????isIndexOrName?=?true;
????????try?{
????????????for?(int?i?=?0;?i?<?books.length;?i++)?{
????????????????if?(name.equals(books[i]))?{
????????????????????showBook(name,?i);
????????????????????break;
????????????????}?else?{
????????????????????throw?new?Exception();
????????????????}
????????????}
????????}?catch?(Exception?e)?{
????????????System.out.println("圖書不存在,請重新輸入:");
????????????String?s?=?sc.next();
????????????find2Name(s);
????????}
????}

????public?static?void?find2Index(int?index)?{
????????isIndexOrName?=?false;
????????try?{
????????????for?(int?i?=?0;?i?<?books.length;?i++)?{
????????????????if?(index?==?i)?{
????????????????????showBook(index);
????????????????????break;
????????????????}?else?if?(index?>?2)?{
????????????????????throw?new?Exception();
????????????????}
????????????}
????????}?catch?(Exception?e)?{
????????????System.out.println("圖書不存在,請重新輸入:");
????????????int?i?=?sc.nextInt();
????????????find2Index(i);
????????}
????}

????/**
?????*?通過書名查找圖書
?????*
?????*?@param?name??書名
?????*?@param?index?書名對應的下標
?????*/
????private?static?void?showBook(String?name,?int?index)?{
????????System.out.println("***查找到以下內容***");
????????if?(books[index].equals(name))?{
????????????System.out.println("第一行代碼");
????????}?else?if?(books[index].equals(name))?{
????????????System.out.println("thinking?in?java");
????????}?else?if?(books[index].equals(name))?{
????????????System.out.println("安卓探索藝術");
????????}
????????System.out.println("***確定借閱嗎?(Y/N)***");
????????String?s?=?sc.next();
????????isBorrow(s);
????}

????/**
?????*?通過圖書序號查找
?????*
?????*?@param?index?圖書的序號
?????*/
????private?static?void?showBook(int?index)?{
????????System.out.println("***查找到以下內容***");
????????System.out.println(books[index]);
????????System.out.println("確認借閱本書嗎?(Y/N)");
????????String?s?=?sc.next();
????????isBorrow(s);
????}

????private?static?void?isBorrow(String?s)?{
????????if?(s.equals("Y")?||?s.equals("y"))?{
????????????System.out.println("恭喜您借閱成功");
????????}?else?if?(s.equals("N")?||?s.equals("n"))?{
????????????System.out.println("您以取消操作!請重新操作!");
????????????if?(flag)?{
????????????????System.out.println("請輸入一個有效查找方式:"+"\n"+"1.按序號查找"+"\n"+"2.按書名查找");
????????????????find2IndexOrName();
????????????}
????????}
????}
}


正在回答

1 回答

都沒有用到多重catch

0 回復 有任何疑惑可以回復我~
#1

慕標5588573 提問者

你把簡單的問題復雜化了,這并不符合面向對象的編程思想
2019-02-25 回復 有任何疑惑可以回復我~

舉報

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

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

進入課程

交作業---模擬結束系統

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

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

幫助反饋 APP下載

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

公眾號

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