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

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

求教啊。。不管怎樣,都是異常報(bào)出。不懂

package exception_test;
import java.util.*;//導(dǎo)入java工具
public class Test_Exception {


/*圖書查找程序:
?* 1.提示用戶輸入,分別按書名及圖書序號(hào)來查找圖書
?* 2.根據(jù)輸入信息進(jìn)行適當(dāng)?shù)漠惓L幚?br />?*/
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?show();
?? ??? ?
?? ??? ?
?? ?}
?? ?public static void books(String bookname){
?? ??? ?String[] book={"論語","高數(shù)","日語","英語","漢語"};
?? ??? ?
?? ??? ?try{
?? ??? ??? ?for(int i =0;i<book.length;i++){
?? ??? ??? ?if(book[i].equals(bookname)){
?? ??? ??? ??? ?System.out.println("book:"+book[i]);
?? ??? ??? ?}else {
?? ??? ??? ??? ?throw new Exception();
?? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}catch (Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("您查詢的書名不存在: "+"\n" + "如有需要請(qǐng)重新選擇:");?? ?
?? ??? ??? ?show();
?? ??? ?}
?? ?}
?? ?public static void bookNum(int BookNum){
?? ??? ?String[] book={"論語","高數(shù)","日語","英語","漢語"};
?? ??? ?try{
?? ??? ??? ?int bookNum =0;
?? ??? ??? ?if(bookNum >0 && bookNum<book.length){
?? ??? ??? ??? ?System.out.println("book:"+book[bookNum-1]);
?? ??? ??? ?}else{
?? ??? ??? ??? ?throw new Exception();
?? ??? ??? ?}
?? ??? ?}catch(Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("您查詢的書籍不存在: "+"\n" + "如有需要請(qǐng)重新選擇:");?? ?
?? ??? ??? ?show();
?? ??? ?}
?? ?}
?? ?public static void bookname(String name){
?? ??? ?Scanner input2 = new Scanner(System.in);
?? ??? ?try{
?? ??? ??? ?System.out.println("請(qǐng)輸入您所需的書名:");
?? ??? ??? ?String name1 =input2.next();
?? ??? ??? ?books(name1);
?? ??? ?}catch(Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("請(qǐng)重新輸入書名:");
?? ??? ??? ?return;
?? ??? ?}
?? ?}
?? ??? ?
?? ?public static void booknum(int num){
?? ??? ?Scanner input3 = new Scanner(System.in);
?? ??? ?try{
?? ??? ??? ?System.out.println("請(qǐng)輸入您所需的書號(hào):");
?? ??? ??? ?int n =input3.nextInt();
?? ??? ??? ?bookNum(n);
?? ??? ?}catch(Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("請(qǐng)重新輸入書號(hào):");
?? ??? ??? ?return;
?? ??? ?}
?? ?}
?? ?
?? ?public static void show(){
?? ??? ?System.out.println("歡迎您來到閱讀快樂吧:書名查詢請(qǐng)選1;序號(hào)查詢請(qǐng)選2");
?? ??? ?Scanner input =new Scanner(System.in);
?? ??? ?int Num =input.nextInt();
?? ??? ?try{
?? ??? ??? if(Num ==1){
?? ??? ??? ?bookname(null);
?? ??? ?? }else if (Num==2){
?? ??? ??? ?booknum(Num);
?? ??? ?? }else{
?? ??? ??? ?? throw new Exception();
?? ??? ?? }
?? ??? }catch(Exception e){
?? ??? ? System.out.println("您輸入的信息有誤,請(qǐng)重新輸入:");
?? ??? ? show();
?? ????? }
???? }

}

正在回答

2 回答

books(String bookname) 方法中

for(int i =0;i<book.length;i++){
?? ??? ??? ?if(book[i].equals(bookname)){
?? ??? ??? ??? ?System.out.println("book:"+book[i]);
?? ??? ??? ?}else {
?? ??? ??? ??? ?throw new Exception();
?? ??? ??? ?}
?? ??? ??? ?}

你這里遍歷了整個(gè)數(shù)組,所以即便你輸入了有的課程,循環(huán)依然會(huì)執(zhí)行,那么必然會(huì)出現(xiàn)不相等的情況,從而執(zhí)行??throw new Exception();


bookNum(int BookNum) 方法中

int bookNum =0;

if(bookNum >0 && bookNum<book.length) 沒有用形參BookNum,所以你輸入什么都會(huì)報(bào)錯(cuò)

另外,你這異常使用的方式很奇葩!程序有很大的可優(yōu)化空間。

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

ygKan 提問者

請(qǐng)問,怎么修改呢?初學(xué)者,不懂。請(qǐng)見諒。謝謝
2019-05-06 回復(fù) 有任何疑惑可以回復(fù)我~
#2

小遁哥 回復(fù) ygKan 提問者

第一個(gè)問題找到后break退出循環(huán) 第二個(gè)問題使用行參去判斷 繼續(xù)學(xué)習(xí),慢慢就會(huì)明白的。
2019-05-06 回復(fù) 有任何疑惑可以回復(fù)我~
#3

ygKan 提問者 回復(fù) 小遁哥

第一個(gè)使用break后,還是不可以。第二個(gè)問題,解決了。謝謝。
2019-05-06 回復(fù) 有任何疑惑可以回復(fù)我~

?public static void bookNum(int BookNum){
?? ??? ?String[] book={"論語","高數(shù)","日語","英語","漢語"};
?? ??? ?try{
?? ??? ??? ?int bookNum =0;
?? ??? ??? ?if(bookNum >0 && bookNum<book.length){
?? ??? ??? ??? ?System.out.println("book:"+book[bookNum-1]);
?? ??? ??? ?}else{
?? ??? ??? ??? ?throw new Exception();
?? ??? ??? ?}
?? ??? ?}catch(Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?System.out.println("您查詢的書籍不存在: "+"\n" + "如有需要請(qǐng)重新選擇:");?? ?
?? ??? ??? ?show();
?? ??? ?}
?? ?}

System.out.println("book:"+book[bookNum-1]);這一行,你的bookNum=0,bookNum-1不就是-1了嗎?肯定報(bào)數(shù)組越界異常啊,另外你參數(shù)int BookNum也沒用上啊

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

ygKan 提問者

謝謝,這個(gè)問題解決了。可是 按書名遍歷時(shí),有異常。不知道怎么解。。
2019-05-07 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

求教啊。。不管怎樣,都是異常報(bào)出。不懂

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

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

幫助反饋 APP下載

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

公眾號(hào)

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