課程
/后端開(kāi)發(fā)
/Java
/Java入門(mén)第三季
你們從控制臺(tái)獲取的漢字 跟 書(shū)名的數(shù)組中的漢字比較相同 是怎么比較的,用漢字書(shū)名 總是出錯(cuò)
2016-12-27
源自:Java入門(mén)第三季 1-9
正在回答
System.out.println("請(qǐng)輸入圖書(shū)名稱:"); String?name?=?new?Scanner(System.in).nextLine().toString(); String[]?books?=?{"xiyouji"?,?"hongloumeng"?,?"zhuxian"?,?"guichuideng"}; boolean?isFind?=?false; for?(String?bookName?:?books)?{ ????if?(bookName.equals(name))?{ ????????System.out.println("book:"?+?name); ????????isFind?=?true; ????????break; ????} } if?(!isFind)?{ ????throw?new?Exception(); }
Adam靜棲 提問(wèn)者
public?void?kindChoice?(int?num)?throws?NameNoMatchException{ switch?(num)?{ case?1: System.out.println("請(qǐng)輸入圖書(shū)名稱:"); String?name?=?(String)new?Scanner(System.in).nextLine().toString(); try?{ nameChoice(name); }?catch?(NameNoMatchException?e)?{ System.out.println("輸入的圖書(shū)名字不存在"); //回滾操作 orderChoice(); } break; case?2: System.out.println("請(qǐng)輸入圖書(shū)序號(hào):"); int?bookNum?=?new?Scanner(System.in).nextInt(); try?{ numChoice(bookNum); }?catch?(NameNoMatchException?e)?{ System.out.println("輸入的圖書(shū)序號(hào)不存在"); orderChoice(); } break; default: throw?new?NameNoMatchException("命令輸入錯(cuò)誤,請(qǐng)根據(jù)提示重新輸入"); } }
重新截了一下kindChoice方法 這樣好看點(diǎn)
public?void?nameChoice(String?name)?throws?NameNoMatchException?{ String[]?books?=?{"xiyouji"?,?"hongloumeng"?,?"zhuxian"?,?"guichuideng"}; boolean?isFind?=?false; for?(String?bookName?:?books)?{ if?(bookName.equals(name))?{ System.out.println("book:"?+?name); isFind?=?true; break; } } if?(!isFind)?{ throw?new?NameNoMatchException(); } }
public void kindChoice (int num) throws NameNoMatchException{
switch (num) {
case 1:
System.out.println("請(qǐng)輸入圖書(shū)名稱:");
String name = (String)new Scanner(System.in).nextLine().toString();
try {
nameChoice(name);
} catch (NameNoMatchException e) {
System.out.println("輸入的圖書(shū)名字不存在");
//回滾操作
orderChoice();
}
break;
case 2:
System.out.println("請(qǐng)輸入圖書(shū)序號(hào):");
int bookNum = new Scanner(System.in).nextInt();
numChoice(bookNum);
System.out.println("輸入的圖書(shū)序號(hào)不存在");
default:
throw new NameNoMatchException("命令輸入錯(cuò)誤,請(qǐng)根據(jù)提示重新輸入");
在kindChoice方法中獲取的控制臺(tái)輸入 漢字比較總是出錯(cuò)
代碼貼出來(lái)吧~
這樣試了還是不行 書(shū)名是從控制臺(tái)通過(guò)scanner獲取的
for(int i=0;i<arr.length;i++){
????str.equals(arr[i]);
舉報(bào)
Java中你必須懂得常用技能,不容錯(cuò)過(guò)的精彩,快來(lái)加入吧
1 回答為什么輸入漢字的名字時(shí)會(huì)無(wú)法執(zhí)行下去
3 回答圖書(shū)名字判斷不了啊
2 回答求解 關(guān)于if判斷漢字equals為false的問(wèn)題
1 回答eclipse漢化問(wèn)題
1 回答那么問(wèn)題來(lái)了,漢字怎么隨機(jī)生成?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-12-28
2016-12-27
重新截了一下kindChoice方法 這樣好看點(diǎn)
2016-12-27
public void kindChoice (int num) throws NameNoMatchException{
switch (num) {
case 1:
System.out.println("請(qǐng)輸入圖書(shū)名稱:");
String name = (String)new Scanner(System.in).nextLine().toString();
try {
nameChoice(name);
} catch (NameNoMatchException e) {
System.out.println("輸入的圖書(shū)名字不存在");
//回滾操作
orderChoice();
}
break;
case 2:
System.out.println("請(qǐng)輸入圖書(shū)序號(hào):");
int bookNum = new Scanner(System.in).nextInt();
try {
numChoice(bookNum);
} catch (NameNoMatchException e) {
System.out.println("輸入的圖書(shū)序號(hào)不存在");
orderChoice();
}
break;
default:
throw new NameNoMatchException("命令輸入錯(cuò)誤,請(qǐng)根據(jù)提示重新輸入");
}
}
在kindChoice方法中獲取的控制臺(tái)輸入 漢字比較總是出錯(cuò)
2016-12-27
代碼貼出來(lái)吧~
2016-12-27
這樣試了還是不行 書(shū)名是從控制臺(tái)通過(guò)scanner獲取的
2016-12-27
for(int i=0;i<arr.length;i++){
????str.equals(arr[i]);
}