百思不得其解,當(dāng)圖書名稱輸入錯(cuò)誤時(shí)不能重新執(zhí)行bookName()方法
package?librarySystem; import?java.util.*; public?class?LibrarySystem?{ ????//自定義NoBookException異常類 ????public?static?class?NoBookException?extends?Exception{ ????????public?NoBookException(){} ????????public?NoBookException(String?message){ ????????????super(message); ????????} ????} ????public?static?String[]?books={"高數(shù)","線代","幾何","生物","編程"}; ????public?static?void?main(String[]?args)?{ ????????System.out.println("序號(hào).\t書名"); ????????for?(int?i=1;i<=5;i++){ ????????????System.out.println(i+".\t\t"+books[i-1]); ????????} ????????LibrarySystem?lib=new?LibrarySystem(); ????????lib.test(); ????} ????public?void?test(){ ????????try?{ ????????????System.out.println("請(qǐng)選擇:1-按照序號(hào)查找圖書;2-按照名稱查找圖書"); ????????????int?i?=?input(); ????????????switch?(i)?{ ????????????????case?1: ????????????????????bookNum(); ????????????????????break; ????????????????case?2: ????????????????????bookName(); ????????????????????break; ????????????????case?-1: ????????????????????test(); ????????????????????break; ????????????????default: ????????????????????throw?new?NoBookException("請(qǐng)輸入1或2:"); ????????????} ????????} ????????catch(NoBookException?e){ ????????????System.out.println(e.getMessage()); ????????????test(); ????????} ????} ????public?void?bookNum(){ ????????????System.out.println("請(qǐng)輸入圖書序號(hào):"); ????????????try?{ ????????????????int?i?=?input(); ????????????????if?(i?>?0?&?i?<=?books.length) ????????????????????System.out.println(books[i-1]); ????????????????else ????????????????????throw?new?NoBookException("請(qǐng)輸入正確的序號(hào)"); ????????????}catch(NoBookException?e){ ????????????????System.out.println(e.getMessage()); ????????????????bookNum(); ????????????} ????} ????public?void?bookName(){ ????????Scanner?sc?=?new?Scanner(System.in); ????????try?{ ????????????System.out.println("請(qǐng)輸入書名:"); ????????????String?book=?sc.next(); ????????????boolean?flag=false; ????????????for?(String?i:books)?{ ????????????????if?(book.equals(i))?{ ????????????????????flag=true; ????????????????????System.out.println(i); ????????????????????break; ????????????????} ????????????} ????????????if(flag=false)?{ ????????????????throw?new?NoBookException("請(qǐng)輸入正確的書名:"); ????????????} ????????}catch?(NoBookException?e){ ????????????System.out.println(e.getMessage()); ????????????bookName(); ????????}catch?(Exception?e){ ????????????e.printStackTrace(); ????????????bookName(); ????????} ????} ????public?int?input(){ ????????Scanner?sc?=?new?Scanner(System.in); ????????try?{ ????????????int?i?=?sc.nextInt(); ????????????return?i; ????????}catch?(Exception?e){ ????????????System.out.println("請(qǐng)輸入整數(shù)"); ????????????sc=new?Scanner(System.in); ????????????return?-1; ????????} ????} }
第61行,當(dāng)圖書名稱輸入錯(cuò)誤時(shí)不能重新執(zhí)行bookName()方法,不知道哪出問題了
2018-12-24
我想問你寫了多長(zhǎng)時(shí)間實(shí)現(xiàn)的
2018-11-12
哈哈 弄明白了,問題出來第74行,flag==false!