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

為了賬號安全,請及時綁定郵箱和手機立即綁定

作業(yè)的疑問

package?sentBook;

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

public?class?sentBook?{

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

	}
	/*歡迎界面?
	?*同時對輸入進行校驗?
	?*/
	public?void?welcome(){
		System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書;");
		int?select?=?0;
		try{
			select?=?new?Scanner(System.in).nextInt();
		}catch(InputMismatchException?e){
			System.out.println("命令輸入錯誤!請按照提示輸入數(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("請按照提示輸入正確命令!");
			this.welcome();
		}
		return;
	}
	/*
	?*?按名稱查書
	?*/
	public?void?sentName(){
		System.out.println("輸入圖書名稱:");
		String?name?=?scan.next();
		String?books?=?Arrays.toString(book);
		if(books.indexOf(name)?!=?-1){
			System.out.println("book:"?+?name);
		}else{
			System.out.println("圖書不存在!");
			this.welcome();
		}
	}
	/*
	?*?按序列號查書
	?*/
	public?void?sentNum(){
		System.out.println("輸入圖書序號:");
		int?num?=?scan.nextInt();
		if(num?<=?book.length+1){
			System.out.println("book:"?+?book[num?-?1]);
		}else{
			System.out.println("圖書不存在!");
			this.welcome();
		}
	}

}

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

輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書;

a

命令輸入錯誤!請按照提示輸入數(shù)字命令!

輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書;

1

輸入圖書名稱:

九型人格

book:九型人格

fuck!

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

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

正在回答

3 回答

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

1 回復 有任何疑惑可以回復我~
#1

sioneden 提問者

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

已修改代碼

package?sentBook;

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

public?class?sentBook?{

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

	}
	/*歡迎界面?
	?*同時對輸入進行校驗?
	?*/
	public?void?welcome(){
		System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書;");
		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("命令輸入錯誤!請按照提示輸入數(shù)字命令!");
			this.welcome();
		}catch(Exception?e){
			System.out.println("請按照提示輸入正確命令!");
			this.welcome();
		}
		return;
	}
	/*
	?*?按名稱查書
	?*/
	public?void?sentName(){
		System.out.println("輸入圖書名稱:");
		String?name?=?scan.next();
		String?books?=?Arrays.toString(book);
		if(books.indexOf(name)?!=?-1){
			System.out.println("book:"?+?name);
		}else{
			System.out.println("圖書不存在!");
			this.welcome();
		}
	}
	/*
	?*?按序列號查書
	?*/
	public?void?sentNum(){
		System.out.println("輸入圖書序號:");
		int?num?=?scan.nextInt();
		if(num?<=?book.length){
			System.out.println("book:"?+?book[num?-?1]);
		}else{
			System.out.println("圖書不存在!");
			this.welcome();
		}
	}

}


0 回復 有任何疑惑可以回復我~

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

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Java入門第三季
  • 參與學習       409764    人
  • 解答問題       4543    個

Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧

進入課程

作業(yè)的疑問

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號