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

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

百思不得其解,當圖書名稱輸入錯誤時不能重新執行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={"高數","線代","幾何","生物","編程"};

????public?static?void?main(String[]?args)?{

????????System.out.println("序號.\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("請選擇:1-按照序號查找圖書;2-按照名稱查找圖書");
????????????int?i?=?input();
????????????switch?(i)?{
????????????????case?1:
????????????????????bookNum();
????????????????????break;
????????????????case?2:
????????????????????bookName();
????????????????????break;
????????????????case?-1:
????????????????????test();
????????????????????break;
????????????????default:
????????????????????throw?new?NoBookException("請輸入1或2:");
????????????}
????????}
????????catch(NoBookException?e){
????????????System.out.println(e.getMessage());
????????????test();
????????}
????}

????public?void?bookNum(){
????????????System.out.println("請輸入圖書序號:");
????????????try?{
????????????????int?i?=?input();
????????????????if?(i?>?0?&?i?<=?books.length)
????????????????????System.out.println(books[i-1]);
????????????????else
????????????????????throw?new?NoBookException("請輸入正確的序號");
????????????}catch(NoBookException?e){
????????????????System.out.println(e.getMessage());
????????????????bookNum();
????????????}
????}

????public?void?bookName(){
????????Scanner?sc?=?new?Scanner(System.in);
????????try?{
????????????System.out.println("請輸入書名:");
????????????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("請輸入正確的書名:");
????????????}
????????}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("請輸入整數");
????????????sc=new?Scanner(System.in);
????????????return?-1;
????????}
????}
}

第61行,當圖書名稱輸入錯誤時不能重新執行bookName()方法,不知道哪出問題了

正在回答

2 回答

我想問你寫了多長時間實現的

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

哈哈 弄明白了,問題出來第74行,flag==false!

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

舉報

0/150
提交
取消

百思不得其解,當圖書名稱輸入錯誤時不能重新執行bookName()方法

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

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

幫助反饋 APP下載

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

公眾號

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