第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

交作業(yè)了?。。?/h1>

package exception_demo;

import java.util.Scanner;

public class Book {


public static void main(String[] args) {

// TODO Auto-generated method stub

Book bookObj = new Book();

bookObj.init();

}

public void init() {

String[] books = {"java","php","python","go"};

int type;

String BookName;

try {

type = searchType();

if(type == 1) {

BookName = searchBookName(books);

}else {

BookName = searchBookNum(books);

}

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

}catch(BookException e) {

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

init();

}catch(Exception e) {

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

init();

}

}

//通過(guò)哪種類型選擇圖書(shū)

public int searchType() throws BookException {

Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象

System.out.print("請(qǐng)輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");

int type = input.nextInt();

if(type == 1 || type == 2) {

return type;

}else{

throw new BookException("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");

}

}

//通過(guò)名稱查找

public String searchBookName(String[] books) throws Exception {

Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象

System.out.println("請(qǐng)輸入圖書(shū)名稱:");

String BookName = input.next();

for(String book :? books) {

if(book.equals(BookName)) {

return BookName;

}

}


throw new Exception("圖書(shū)不存在!");

}

//通過(guò)序號(hào)查找

public String searchBookNum(String[] books) throws Exception {

Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象

System.out.println("請(qǐng)輸入圖書(shū)序號(hào):");

int BookNum = input.nextInt();

if(BookNum < books.length && BookNum >= 0) {

return books[BookNum-1];

}

throw new Exception("圖書(shū)不存在!");

}


}



package exception_demo;

public class BookException extends Exception{

public BookException(String message) {

super(message);

}

}


正在回答

2 回答

兄弟 我試了一下你的代碼,有個(gè)問(wèn)題

下面這段里,當(dāng)我int type輸入非數(shù)字時(shí),比如我控制臺(tái)打一個(gè)a,為什么沒(méi)有報(bào)InputMismatchException,你的代碼運(yùn)行結(jié)果是顯示null,然后重新開(kāi)始,這個(gè)我想不通啊


//通過(guò)哪種類型選擇圖書(shū)

public int searchType() throws BookException {

Scanner input = new Scanner(System.in);//創(chuàng)建scanner對(duì)象

System.out.print("請(qǐng)輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");

int type = input.nextInt();

if(type == 1 || type == 2) {

return type;

}else{

throw new BookException("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");

}

}


0 回復(fù) 有任何疑惑可以回復(fù)我~

這個(gè)是不是沒(méi)法實(shí)現(xiàn)報(bào)錯(cuò)之后重新輸入?

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕瓜7049329

我錯(cuò)了。。。原來(lái)還可以這樣 init()里面再init()
2020-03-13 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
Java入門(mén)第三季
  • 參與學(xué)習(xí)       409775    人
  • 解答問(wèn)題       4546    個(gè)

Java中你必須懂得常用技能,不容錯(cuò)過(guò)的精彩,快來(lái)加入吧

進(jìn)入課程

交作業(yè)了?。。?/h1> 我要回答 關(guān)注問(wèn)題

微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)