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

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

來交作業了

import java.util.Scanner;

public class Initial {

String? [] BookList=new String[] {"高數","Java","數據結構","C++","計算機網絡"};

public static void main(String[] args) {

Initial init=new Initial();

init.start();

}


public void start() {

? ?int a=1;

ChooseMethod();

}


public void ChooseMethod() {

System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");

Scanner methodinput=new Scanner(System.in);

try {

int method=methodinput.nextInt();

if(method==1)

SearchByName();

if (method==2)

SearchByNumber();

} catch (NameException e) {

System.out.println(e.getMessage());

start();

}catch (NumberException e) {

System.out.println(e.getMessage());

start();

}catch (Exception e) {

System.out.println("命令輸入錯誤!請按照提示輸入數字命令!");

start();

}

}


public void SearchByNumber() throws NumberException{

System.out.println("輸入圖書序號:");

Scanner BookNumberInput=new Scanner(System.in);

int BookNumber = 0;

try {

BookNumber = BookNumberInput.nextInt();

} catch (Exception e) {

System.out.println("命令輸入錯誤!請按照提示輸入數字命令!");

SearchByNumber();

}

if(BookNumber<=BookList.length)

System.out.println("book:"+BookList[BookNumber-1]);

else

throw new NumberException();

}


public void SearchByName() throws NameException{

System.out.println("輸入圖書名稱:");

Scanner BooknameInput=new Scanner(System.in);

String Bookname=BooknameInput.nextLine();

Boolean check=false;

for(int i=0;i<BookList.length;i++) {

if(BookList[i].equals(Bookname))

{

System.out.println("book:"+Bookname);

check=true;

}

}

if(check==false)

throw new NameException();

}

}


public class NameException extends Exception{

NameException(){

super("圖書不存在!");

}

}


public class NumberException extends Exception{

public NumberException() {

super("圖書不存在!");

}

}










正在回答

1 回答

厲害,向你學習

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

舉報

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

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

進入課程

來交作業了

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

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

幫助反饋 APP下載

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

公眾號

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