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

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

Scanner中用nextInt(輸入非數(shù)字),產(chǎn)生異常后再執(zhí)行輸入時(shí)的現(xiàn)象

package?test1;

import?java.util.Scanner;

public?class?test?{
	Scanner?cmd?=?new?Scanner(System.in);
	String?[]?bookName?=?{"高數(shù)","數(shù)據(jù)結(jié)構(gòu)"};
	
	public??int?inputCommand(){
		int?command;
		try?{
			command?=?cmd.nextInt();
			return?command;
		}?catch?(Exception?e)?{
			cmd?=?new?Scanner(System.in);
			return?-1;
		}
	}
	
	public?String?searchSerial()?throws?Exception{
		System.out.println("輸入序號(hào):");
		while(true)
		{
		try?{
		int?serial?=?cmd.nextInt();
			return?bookName[serial];
		}catch(Exception?e)?{
//			cmd?=?new?Scanner(System.in);??//如果注釋掉這一行就會(huì)發(fā)生圖中的錯(cuò)誤
			throw?new?Exception("圖書不存在?s");
		}
		}
	}
	
	public?static?void?main(String[]?args)?{
		test?t?=new?test();
		while(true)
		{
			try?{
				System.out.println("輸入命令,1-按名稱查找,2-按序號(hào)查找");
				int?i=t.inputCommand();
				switch?(i)
				{
				case?2:
				{
					System.out.println("book:"+t.searchSerial());
					break;
				}
				case?-1:
				{
					System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
					continue;
				}
				default:
				{
					System.out.println("命令輸入錯(cuò)誤!");
					continue;
				}
				}
				break;
			}catch(Exception?e)
			{
				System.out.println(e.getMessage());
				continue;
			}
		}
	}
}

注釋的那一行cmd?=?new?Scanner(System.in);


http://img1.sycdn.imooc.com//5a916bca0001b56701930185.jpg










取消注釋后結(jié)果

http://img1.sycdn.imooc.com//5a916c40000190ab01970158.jpg


請(qǐng)大神給我講講內(nèi)部的原理,個(gè)人理解是輸入的值還被保存著但是不知道為啥在執(zhí)行一次就沒了。。

正在回答

1 回答

在選擇圖書的時(shí)候拋出異常后,將異常捕獲后你有拋到外層了 然后在main的switch中被捕獲

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

Yme 提問者

cmd = new Scanner(System.in); //如果注釋掉這一行就會(huì)發(fā)生圖中的錯(cuò)誤 如果注釋掉這一行,會(huì)彈出選擇圖書的異常,而且還會(huì)自己拋一次類型的異常,再出現(xiàn)輸入選擇類型 還是有點(diǎn)疑惑
2018-02-25 回復(fù) 有任何疑惑可以回復(fù)我~
#2

慕仔1173296 回復(fù) Yme 提問者

我也有這個(gè)疑惑,請(qǐng)問樓主解決了么
2018-06-05 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

Scanner中用nextInt(輸入非數(shù)字),產(chǎn)生異常后再執(zhí)行輸入時(shí)的現(xiàn)象

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

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

幫助反饋 APP下載

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

公眾號(hào)

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