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

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

第一章的作業(yè)參考修改一下,程序運(yùn)行有問(wèn)題,請(qǐng)大家指教!

?

package com.imooc.test;
import java.util.Scanner;
public class LibraryManager {
?//創(chuàng)建Scanner對(duì)象console
?private static Scanner console = new Scanner(System.in);
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??//定義”圖書(shū)“數(shù)組
????String[] books = { "C語(yǔ)言", "數(shù)據(jù)結(jié)構(gòu)", "匯編語(yǔ)言", "高數(shù)", "大學(xué)語(yǔ)文", "毛概" };
????while (true) {
?????System.out.println("歡迎來(lái)到圖書(shū)管理系統(tǒng)!");
?????System.out.println("輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");
?????String book;
?????try {
??????//取得整型命令
??????int command =console.nextInt();
??????//根據(jù)不同命令值,進(jìn)行不同操作
??????switch (command) {
??????case 1://按照?qǐng)D書(shū)名稱選擇圖書(shū)
???????book = getBookByName(books);
???????System.out.println("book:" + book);
???????break;//退出循環(huán),進(jìn)行循環(huán)后面的代碼
??????case 2://按照?qǐng)D書(shū)序號(hào)(數(shù)組下標(biāo))選擇圖書(shū)
???????book = getBookByNumber(books);
???????System.out.println("book:" + book);
???????break;
??????/*case -1://返回值為-1,說(shuō)明輸入有誤
???????System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
???????continue;//結(jié)束本次循環(huán),直接進(jìn)入下次循環(huán)*/
??????default://其他值的命令均認(rèn)為是錯(cuò)誤命令
???????System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
???????continue;
??????}
??????break;//退出程序
?????} catch (Exception bne) {
??????//捕獲”圖書(shū)不存在異?!皶r(shí),要求重新輸入命令
??????System.out.println(bne.getMessage());
??????continue;
?????}
????}
???}

???//按照?qǐng)D書(shū)名稱查詢圖書(shū)
???private static String getBookByName(String[] books)
?????throws Exception {
????System.out.println("輸入圖書(shū)名稱:");
????//獲取輸入的圖書(shū)名稱
????String name = console.next();
????for (int i = 0; i < books.length; i++) {
?????if (name.equals(books[i]))
??????//輸入的名稱與某一圖書(shū)名稱匹配,返回該圖書(shū)
??????return books[i];
????}
????//若無(wú)匹配,拋出”圖書(shū)不存在異常“
????throw new Exception("圖書(shū)不存在!");
???}

???//根據(jù)圖書(shū)序號(hào)(數(shù)組下標(biāo))查詢圖書(shū)
???private static String getBookByNumber(String[] books)
?????throws Exception {
????//while (true) {
?????System.out.println("輸入圖書(shū)序號(hào):");
?????try {
??????//獲取輸入的圖書(shū)序號(hào)(數(shù)組下標(biāo))
??????int index = console.nextInt();
??????//若返回值為-1
??????/*if(index == -1){
???????System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
???????continue;
??????}*/
??????//若不出現(xiàn)”數(shù)組下標(biāo)越界異常“,則返回相應(yīng)位置的圖書(shū)
??????String book = books[index];
??????return book;
?????} catch (ArrayIndexOutOfBoundsException e) {
??????//輸入的序號(hào)不存在(引發(fā)”數(shù)組下標(biāo)越界異?!埃?,則拋出”圖書(shū)不存在異?!?br />??????Exception bookNotExists = new Exception("圖書(shū)不存在!");
??????bookNotExists.initCause(e);
??????throw bookNotExists;
?????}
????}
??}?

正在回答

2 回答

private static Scanner console = new Scanner(System.in);為什么要在Scanner前面加private static

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

金乘三

請(qǐng)問(wèn)一下 System.out.println(bne.getMessage()); 這段代碼的作用是什么 并沒(méi)看到 哪定義了這個(gè)bne.getMessage()方法
2016-08-09 回復(fù) 有任何疑惑可以回復(fù)我~

package com.imooc.proj_1;


import java.util.Scanner;


public class BookManagerEasy {

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


public static void main(String[] args) {

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

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

while (true) {

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

String book;

try {

// 取得整型命令

int command = inputCommand();

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

switch (command) {

case 1:// 按照?qǐng)D書(shū)名稱選擇圖書(shū)

book = getBookByName(books);

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

break;

case 2:// 按照?qǐng)D書(shū)序號(hào)(數(shù)組下標(biāo))選擇圖書(shū)

book = getBookByNumber(books);

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

break;

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

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

continue;

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

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

continue;

}

break;// 退出程序

} catch (Exception bne) {

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

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

continue;

}

}

}


// 按照?qǐng)D書(shū)名稱查詢圖書(shū)

private static String getBookByName(String[] books) throws Exception {

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

// 獲取輸入的圖書(shū)名稱

String name = console.next();

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

if (name.equals(books[i]))

// 輸入的名稱與某一圖書(shū)名稱匹配,返回該圖書(shū)

return books[i];

}

// 若無(wú)匹配,拋出”圖書(shū)不存在異?!?/p>

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

}


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

private static String getBookByNumber(String[] books) throws Exception {

while (true) {

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

try {

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

int index = inputCommand();

// 若返回值為-1

if (index == -1) {

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

continue;

}

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

String book = books[index];

return book;

} catch (ArrayIndexOutOfBoundsException e) {

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

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

bookNotExists.initCause(e);

throw bookNotExists;

}

}

}


// 從控制臺(tái)輸入命令,用于輸入命令和輸入圖書(shū)序號(hào)

private static int inputCommand() {

int command;

try {

command = console.nextInt();

return command;

} catch (Exception e) {

// 若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出”錯(cuò)誤命令異常“

console = new Scanner(System.in);

// 返回-1

return -1;

}

}

}


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

慕粉3170877 提問(wèn)者

這是???為什么要添加上后面的那一段? // 從控制臺(tái)輸入命令,用于輸入命令和輸入圖書(shū)序號(hào) private static int inputCommand() { int command; try { command = console.nextInt(); return command; } catch (Exception e) { // 若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出”錯(cuò)誤命令異常“ console = new Scanner(System.in); // 返回-1 return -1; } } }
2016-05-12 回復(fù) 有任何疑惑可以回復(fù)我~
#2

qq_隨想之風(fēng)_03202251 回復(fù) 慕粉3170877 提問(wèn)者

他這樣這一段代碼有兩個(gè)作用,首先判斷輸入的命令是(1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)),可以用,另外在按照序號(hào)查找圖書(shū)的時(shí)候也可以用(判斷輸入的是否是數(shù)字),只需要調(diào)用這個(gè)方法,不需要重復(fù)寫(xiě),很方便的
2016-07-17 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

第一章的作業(yè)參考修改一下,程序運(yùn)行有問(wèn)題,請(qǐng)大家指教!

我要回答 關(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)