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

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

作業(yè)的疑問(wèn)

package?sentBook;

import?java.util.Arrays;
import?java.util.InputMismatchException;
import?java.util.Scanner;

public?class?sentBook?{

	String[]?book?=?new?String[]{"你好,中國(guó)",?"九型人格",?"Java123",?"面向?qū)ο缶幊?,?"美文如歌"};
	Scanner?scan?=?new?Scanner(System.in);
	public?static?void?main(String[]?args)?{
		//?TODO?Auto-generated?method?stub
		sentBook?sent?=?new?sentBook();
		sent.welcome();

	}
	/*歡迎界面?
	?*同時(shí)對(duì)輸入進(jìn)行校驗(yàn)?
	?*/
	public?void?welcome(){
		System.out.println("輸入命令:1-按照名稱(chēng)查找圖書(shū);2-按照序號(hào)查找圖書(shū);");
		int?select?=?0;
		try{
			select?=?new?Scanner(System.in).nextInt();
		}catch(InputMismatchException?e){
			System.out.println("命令輸入錯(cuò)誤!請(qǐng)按照提示輸入數(shù)字命令!");
			this.welcome();
		}
		try{
			if(select?==?1?){
				this.sentName();
			}else?if(select?==?2){
				this.sentNum();
			}else?if(select?==?0){
				System.out.println("fuck!");
			}
			else{
				throw?new?Exception();
			}
		}catch(Exception?e){
			System.out.println("請(qǐng)按照提示輸入正確命令!");
			this.welcome();
		}
		return;
	}
	/*
	?*?按名稱(chēng)查書(shū)
	?*/
	public?void?sentName(){
		System.out.println("輸入圖書(shū)名稱(chēng):");
		String?name?=?scan.next();
		String?books?=?Arrays.toString(book);
		if(books.indexOf(name)?!=?-1){
			System.out.println("book:"?+?name);
		}else{
			System.out.println("圖書(shū)不存在!");
			this.welcome();
		}
	}
	/*
	?*?按序列號(hào)查書(shū)
	?*/
	public?void?sentNum(){
		System.out.println("輸入圖書(shū)序號(hào):");
		int?num?=?scan.nextInt();
		if(num?<=?book.length+1){
			System.out.println("book:"?+?book[num?-?1]);
		}else{
			System.out.println("圖書(shū)不存在!");
			this.welcome();
		}
	}

}

代碼如圖所示,現(xiàn)在有一個(gè)問(wèn)題,具體的輸出如下:

輸入命令:1-按照名稱(chēng)查找圖書(shū);2-按照序號(hào)查找圖書(shū);

a

命令輸入錯(cuò)誤!請(qǐng)按照提示輸入數(shù)字命令!

輸入命令:1-按照名稱(chēng)查找圖書(shū);2-按照序號(hào)查找圖書(shū);

1

輸入圖書(shū)名稱(chēng):

九型人格

book:九型人格

fuck!

最開(kāi)始是因?yàn)榘l(fā)現(xiàn)一個(gè)問(wèn)題,如果第一次輸入錯(cuò)誤,那么在正確的輸入以及查詢(xún)結(jié)束后,會(huì)繼續(xù)循環(huán)一次查詢(xún)。利用斷點(diǎn)調(diào)試發(fā)現(xiàn),第一次輸入錯(cuò)誤并之后正確執(zhí)行了一次查詢(xún)后,會(huì)繼續(xù)對(duì)輸入的select值進(jìn)行判斷,而且是默認(rèn)的0。如果把這個(gè)默認(rèn)的select賦值去掉的話,程序會(huì)報(bào)錯(cuò)。

想請(qǐng)教一下,怎樣才能在正確執(zhí)行一次查詢(xún)后直接結(jié)束程序,而不是繼續(xù)對(duì)select值進(jìn)行判斷。

正在回答

3 回答

你的代碼錯(cuò)了,第一步你輸入a后拋出異常,但被你捕獲了,所以先執(zhí)行了welcome(),再接著往下執(zhí)行,也就是說(shuō)你輸入一個(gè)a時(shí),你的if...else if同樣還是被執(zhí)行了

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

sioneden 提問(wèn)者

多謝提示!錯(cuò)誤已改正
2015-01-19 回復(fù) 有任何疑惑可以回復(fù)我~

已修改代碼

package?sentBook;

import?java.util.Arrays;
import?java.util.InputMismatchException;
import?java.util.Scanner;

public?class?sentBook?{

	String[]?book?=?new?String[]{"你好,中國(guó)",?"九型人格",?"Java123",?"面向?qū)ο缶幊?,?"美文如歌"};
	Scanner?scan?=?new?Scanner(System.in);
	public?static?void?main(String[]?args)?{
		//?TODO?Auto-generated?method?stub
		sentBook?sent?=?new?sentBook();
		sent.welcome();

	}
	/*歡迎界面?
	?*同時(shí)對(duì)輸入進(jìn)行校驗(yàn)?
	?*/
	public?void?welcome(){
		System.out.println("輸入命令:1-按照名稱(chēng)查找圖書(shū);2-按照序號(hào)查找圖書(shū);");
		int?select?=?0;
		try{
			select?=?new?Scanner(System.in).nextInt();
			if(select?==?1?){
				this.sentName();
			}else?if(select?==?2){
				this.sentNum();
			}else?if(select?==?0){
				System.out.println("fuck!");
			}
			else{
				throw?new?Exception();
			}
		}catch(InputMismatchException?e){
			System.out.println("命令輸入錯(cuò)誤!請(qǐng)按照提示輸入數(shù)字命令!");
			this.welcome();
		}catch(Exception?e){
			System.out.println("請(qǐng)按照提示輸入正確命令!");
			this.welcome();
		}
		return;
	}
	/*
	?*?按名稱(chēng)查書(shū)
	?*/
	public?void?sentName(){
		System.out.println("輸入圖書(shū)名稱(chēng):");
		String?name?=?scan.next();
		String?books?=?Arrays.toString(book);
		if(books.indexOf(name)?!=?-1){
			System.out.println("book:"?+?name);
		}else{
			System.out.println("圖書(shū)不存在!");
			this.welcome();
		}
	}
	/*
	?*?按序列號(hào)查書(shū)
	?*/
	public?void?sentNum(){
		System.out.println("輸入圖書(shū)序號(hào):");
		int?num?=?scan.nextInt();
		if(num?<=?book.length){
			System.out.println("book:"?+?book[num?-?1]);
		}else{
			System.out.println("圖書(shū)不存在!");
			this.welcome();
		}
	}

}


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

if...else if...本身就是正確執(zhí)行一次后就不再執(zhí)行別的判斷了,所以你這邊的問(wèn)題應(yīng)該是邏輯錯(cuò)誤

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

舉報(bào)

0/150
提交
取消
Java入門(mén)第三季
  • 參與學(xué)習(xí)       409764    人
  • 解答問(wèn)題       4543    個(gè)

Java中你必須懂得常用技能,不容錯(cuò)過(guò)的精彩,快來(lái)加入吧

進(jìn)入課程

作業(yè)的疑問(wèn)

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

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

幫助反饋 APP下載

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

公眾號(hào)

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