public?class?BorrowBook?{
????public?static?String[]?books?=?{"第一行代碼",?"thinking?in?java",?"安卓探索藝術(shù)"};
????public?static?Scanner?sc;
????public?static?boolean?flag?=?true;
????public?static?boolean?isIndexOrName?=?false;
????public?static?void?main(String[]?args)?{
????????System.out.println("***歡迎來(lái)到模擬圖書(shū)系統(tǒng)***");
????????System.out.println("請(qǐng)輸入您要查找圖書(shū)的方式:1.按圖書(shū)序號(hào)查找"?+?"\t"?+?"2.按圖書(shū)名稱(chēng)查找");
????????System.out.println("請(qǐng)輸入一個(gè)有效查找方式:");
????????find2IndexOrName();
????}
????private?static?void?find2IndexOrName()?{
????????sc?=?new?Scanner(System.in);
????????int?input?=?sc.nextInt();
????????try?{
????????????if?(1?==?input)?{
????????????????System.out.println("請(qǐng)輸入有效圖書(shū)序號(hào):");
????????????????int?index?=?sc.nextInt();
????????????????find2Index(index);
????????????}?else?if?(2?==?input)?{
????????????????System.out.println("請(qǐng)輸入正確的書(shū)名:");
????????????????String?name?=?sc.next();
????????????????find2Name(name);
????????????}?else?{
????????????????throw?new?Exception();
????????????}
????????}?catch?(Exception?e)?{
????????????System.out.println("請(qǐng)輸入正確指令:");
????????????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("圖書(shū)不存在,請(qǐng)重新輸入:");
????????????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("圖書(shū)不存在,請(qǐng)重新輸入:");
????????????int?i?=?sc.nextInt();
????????????find2Index(i);
????????}
????}
????/**
?????*?通過(guò)書(shū)名查找圖書(shū)
?????*
?????*?@param?name??書(shū)名
?????*?@param?index?書(shū)名對(duì)應(yīng)的下標(biāo)
?????*/
????private?static?void?showBook(String?name,?int?index)?{
????????System.out.println("***查找到以下內(nèi)容***");
????????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("安卓探索藝術(shù)");
????????}
????????System.out.println("***確定借閱嗎?(Y/N)***");
????????String?s?=?sc.next();
????????isBorrow(s);
????}
????/**
?????*?通過(guò)圖書(shū)序號(hào)查找
?????*
?????*?@param?index?圖書(shū)的序號(hào)
?????*/
????private?static?void?showBook(int?index)?{
????????System.out.println("***查找到以下內(nèi)容***");
????????System.out.println(books[index]);
????????System.out.println("確認(rèn)借閱本書(shū)嗎?(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("您以取消操作!請(qǐng)重新操作!");
????????????if?(flag)?{
????????????????System.out.println("請(qǐng)輸入一個(gè)有效查找方式:"+"\n"+"1.按序號(hào)查找"+"\n"+"2.按書(shū)名查找");
????????????????find2IndexOrName();
????????????}
????????}
????}
}
2019-02-25
都沒(méi)有用到多重catch