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

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

老師作業(yè)參考里的代碼。那個while(true),這句判斷的是什么啊。才定義了一個數(shù)組,都沒開始獲取用戶信息呢?

import java.util.Scanner;

public class BookManagerEasy {

private static Scanner console = new Scanner(System.in);


public static void main(String[] args) {

//定義”圖書“數(shù)組

String[] books = { "C語言", "數(shù)據(jù)結(jié)構(gòu)", "匯編語言", "高數(shù)", "大學(xué)語文", "毛概" };


while (true) {

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

String book;

try {

//取得整型命令

int command = inputCommand();

//根據(jù)不同命令值,進(jìn)行不同操作

switch (command) {

case 1://按照圖書名稱選擇圖書

book = getBookByName(books);

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

break;

case 2://按照圖書序號(數(shù)組下標(biāo))選擇圖書

book = getBookByNumber(books);

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

break;

case -1://返回值為-1,說明輸入有誤

System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");

continue;

default://其他值的命令均認(rèn)為是錯誤命令

System.out.println("命令輸入錯誤!");

continue;

}

break;//退出程序

} catch (Exception bne) {

//捕獲”圖書不存在異?!皶r,要求重新輸入命令

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

continue;

}?

}

}


正在回答

4 回答

如果不加上while循環(huán)的話,程序從頭到尾只執(zhí)行一次,但是加上while循環(huán),且條件為true,當(dāng)中間的輸入有誤時,就能無限循環(huán)下去了。

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

無限循環(huán)


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

求解答啊

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


private static String getBookByName(String[] books)throws Exception //按照圖書名稱查詢圖書

{

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

//獲取輸入的圖書名稱

String name = it.next(); //獲取圖書名

for (int i = 0; i < books.length; i++) { //遍歷圖書名數(shù)組

if (name.equals(books[i])) //輸入的名稱與某一圖書名稱匹配,返回該圖書

return books[i];

}

//若無匹配,拋出”圖書不存在異?!?/p>

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

}


//根據(jù)圖書序號(數(shù)組下標(biāo))查詢圖書

private static String getBookByNumber(String[] books)

throws Exception {

while (true) {

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

try {

//獲取輸入的圖書序號(數(shù)組下標(biāo))

int index = inputCommand();

//若返回值為-1

if(index == -1){

System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");

continue;

}

//若不出現(xiàn)”數(shù)組下標(biāo)越界異?!?,則返回相應(yīng)位置的圖書

String book = books[index];

return book;

} catch (ArrayIndexOutOfBoundsException e) {

//輸入的序號不存在(引發(fā)”數(shù)組下標(biāo)越界異?!埃?,則拋出”圖書不存在異常“

Exception bookNotExists = new Exception("圖書不存在!");

bookNotExists.initCause(e);

throw bookNotExists;

}

}

}


//從控制臺輸入命令,用于輸入命令和輸入圖書序號

private static int inputCommand(){

int command;

try {

command = it.nextInt();

return command;

} catch (Exception e) {

//若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出”錯誤命令異?!?/p>

it = new Scanner(System.in);

//返回-1

return -1;

}

}

}


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

舉報

0/150
提交
取消

老師作業(yè)參考里的代碼。那個while(true),這句判斷的是什么啊。才定義了一個數(shù)組,都沒開始獲取用戶信息呢?

我要回答 關(guān)注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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