課程
/后端開發(fā)
/Java
/Java入門第三季
代碼有拋出異常,但是運行是沒有顯示異常語句,是什么情況
2016-05-22
源自:Java入門第三季 1-9
正在回答
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ù)組?? ??? ?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;?? ??? ??? ?} ?? ??? ?}?? ?}?? ?//按照圖書名稱查詢圖書?? ?private static String getBookByName(String[] books)?? ??? ??? ?throws Exception {?? ??? ?System.out.println("輸入圖書名稱:");?? ??? ?//獲取輸入的圖書名稱?? ??? ?String name = console.next();?? ??? ?for (int i = 0; i < books.length; i++) {?? ??? ??? ?if (name.equals(books[i]))?? ??? ??? ??? ?//輸入的名稱與某一圖書名稱匹配,返回該圖書?? ??? ??? ??? ?return books[i];?? ??? ?}?? ??? ?//若無匹配,拋出”圖書不存在異?!?br />?? ??? ?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)越界異?!埃瑒t拋出”圖書不存在異?!?br />?? ??? ??? ??? ?Exception bookNotExists = new Exception("圖書不存在!");?? ??? ??? ??? ?bookNotExists.initCause(e);?? ??? ??? ??? ?throw bookNotExists;?? ??? ??? ?}?? ??? ?}?? ?}?? ?//從控制臺輸入命令,用于輸入命令和輸入圖書序號?? ?private static int inputCommand(){?? ??? ?int command;?? ??? ?try {?? ??? ??? ?command = console.nextInt();?? ??? ??? ?return command;?? ??? ?} catch (Exception e) {?? ??? ??? ?//若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出”錯誤命令異?!?br />?? ??? ??? ?console = new Scanner(System.in);?? ??? ??? ?//返回-1?? ??? ??? ?return -1;?? ??? ?}?? ?}}
Autismdj 提問者
慕粉3402886
怒放的生命012 回復(fù) 慕粉3402886
wshyzx
package imooc.com;
import java.util.Scanner;
public class Name {
public Name() {
}
public void findName(String[] book) throws Exception {
System.out.println("請輸入圖書名:");
Scanner scan = new Scanner(System.in);
try {
String name = scan.next();
for (int i = 0; i < book.length; i++) {
if (name.equals(book[i]))
System.out.println("book:" + book[i]);
} catch (Exception e) {
throw new Exception("圖書不存在!");
public void findId(String[] book) throws Exception {
System.out.println("請輸入圖書序號:");
int i = scan.nextInt();
if (i >= 0 && i < book.length) {
System.out.println("book:"+book[i]);
********************************
public class Main {
public static void main(String[] args) {
String[] book = { "高數(shù)", "數(shù)據(jù)結(jié)構(gòu)", "英語", "c++" };
while (true) {
int i;
System.out.println("請按照提示輸入:1-根據(jù)序號查書,2-根據(jù)書名查書");
i = scan.nextInt();
Name n = new Name();
switch (i) {
case 1:
n.findId(book);
System.out.println(e.getMessage());
continue;
break;
case 2:
n.findName(book);
default:
System.out.println("命令輸入錯誤,請根據(jù)提示輸入!");
可以看看代碼嗎?
試試在軟件上自己敲一遍
舉報
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
2 回答問題:為啥都是拋出異常,大家都沒有處理異常,但是一個會執(zhí)行一個不會執(zhí)行啊?
1 回答異常的拋出
4 回答什么叫try塊中的語句是如何拋出這個異常的啊
1 回答只顯示:方法執(zhí)行了 -1,沒有提示說“循環(huán)拋出異常”?
2 回答JAVA拋出異常和自定義異常
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-05-24
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ù)組
?? ??? ?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;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?//按照圖書名稱查詢圖書
?? ?private static String getBookByName(String[] books)
?? ??? ??? ?throws Exception {
?? ??? ?System.out.println("輸入圖書名稱:");
?? ??? ?//獲取輸入的圖書名稱
?? ??? ?String name = console.next();
?? ??? ?for (int i = 0; i < books.length; i++) {
?? ??? ??? ?if (name.equals(books[i]))
?? ??? ??? ??? ?//輸入的名稱與某一圖書名稱匹配,返回該圖書
?? ??? ??? ??? ?return books[i];
?? ??? ?}
?? ??? ?//若無匹配,拋出”圖書不存在異?!?br />?? ??? ?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)越界異?!埃瑒t拋出”圖書不存在異?!?br />?? ??? ??? ??? ?Exception bookNotExists = new Exception("圖書不存在!");
?? ??? ??? ??? ?bookNotExists.initCause(e);
?? ??? ??? ??? ?throw bookNotExists;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?//從控制臺輸入命令,用于輸入命令和輸入圖書序號
?? ?private static int inputCommand(){
?? ??? ?int command;
?? ??? ?try {
?? ??? ??? ?command = console.nextInt();
?? ??? ??? ?return command;
?? ??? ?} catch (Exception e) {
?? ??? ??? ?//若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出”錯誤命令異?!?br />?? ??? ??? ?console = new Scanner(System.in);
?? ??? ??? ?//返回-1
?? ??? ??? ?return -1;
?? ??? ?}
?? ?}
}
2016-05-24
package imooc.com;
import java.util.Scanner;
public class Name {
public Name() {
}
public void findName(String[] book) throws Exception {
System.out.println("請輸入圖書名:");
Scanner scan = new Scanner(System.in);
try {
String name = scan.next();
for (int i = 0; i < book.length; i++) {
if (name.equals(book[i]))
System.out.println("book:" + book[i]);
}
} catch (Exception e) {
throw new Exception("圖書不存在!");
}
}
public void findId(String[] book) throws Exception {
System.out.println("請輸入圖書序號:");
Scanner scan = new Scanner(System.in);
try {
int i = scan.nextInt();
if (i >= 0 && i < book.length) {
System.out.println("book:"+book[i]);
}
} catch (Exception e) {
throw new Exception("圖書不存在!");
}
}
}
********************************
package imooc.com;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] book = { "高數(shù)", "數(shù)據(jù)結(jié)構(gòu)", "英語", "c++" };
while (true) {
int i;
System.out.println("請按照提示輸入:1-根據(jù)序號查書,2-根據(jù)書名查書");
try {
Scanner scan = new Scanner(System.in);
i = scan.nextInt();
Name n = new Name();
switch (i) {
case 1:
try {
n.findId(book);
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
}
break;
case 2:
try {
n.findName(book);
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
}
break;
default:
break;
}
} catch (Exception e) {
System.out.println("命令輸入錯誤,請根據(jù)提示輸入!");
continue;
}
}
}
}
2016-05-23
可以看看代碼嗎?
2016-05-22
試試在軟件上自己敲一遍