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

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

異常處理之借書系統(tǒng)編寫,為什么用foreach()遍歷引用類型數(shù)組,遍歷對(duì)象一欄也就是數(shù)組名books會(huì)報(bào)錯(cuò)?但是用for()語(yǔ)句來遍歷數(shù)組books就不會(huì)報(bào)錯(cuò),且數(shù)組前必須加public static?求大神解,在線等急。

package?library1;

public?class?Book?{
	int?num;
	String?name;
	public?Book(int?num,String?name){
		this.num=num;
		this.name=name;
	}
	?Book[]books={new?Book(1,"數(shù)據(jù)結(jié)構(gòu)"),
			new?Book(2,"線性數(shù)學(xué)"),new?Book(3,"高等數(shù)學(xué)")};

}
package?library1;

import?java.util.Scanner;

public?class?Library?{
	public?void?library(){
		System.out.println("輸入命令:1.按照名稱查找圖書"
	+"2.按照序號(hào)查找圖書");
		try{
		Scanner?sc=new?Scanner(System.in);
		int?a=sc.nextInt();
		if(a==1){
			library1();
		}else?if(a==2){
			library2();
		}else{
			System.out.println("您輸入的數(shù)據(jù)有誤,請(qǐng)重新輸入(1或2)");
		}
		}catch(Exception?e){
			System.out.println("命令輸入有誤,請(qǐng)?zhí)崾据斎朊?);
			library();//重新調(diào)用方法,實(shí)現(xiàn)數(shù)據(jù)回滾
		}
	}
	//按照?qǐng)D書名稱查找
	public?void?library1(){
		System.out.println("請(qǐng)輸入圖書名稱:");
		try{
		Scanner?sc=new?Scanner(System.in);
		String?b=sc.next();
		for(Book?book:books){
			if(book.name.equals(b)){
				System.out.println("books:"+book.name);
				break;
			}
		}
		}catch(Exception?e){
			System.out.println("圖書不存在!");
			library1();
		}
	}
	//按照?qǐng)D書序號(hào)查找
	public?void?library2(){
		System.out.println("請(qǐng)輸入圖書名稱:");
		try{
		Scanner?sc=new?Scanner(System.in);
		int?c=sc.nextInt();
		for(Book?book:books){
			if(book.num==c){
				System.out.println("books:"+book.name);
				break;
			}
		}
		}catch(Exception?e){
			System.out.println("圖書不存在!");
			library2();
		}
	}
	

}
package?library1;

public?class?Initail?{

	public?static?void?main(String[]?args)?{
		//?TODO?自動(dòng)生成的方法存根
		System.out.println("歡迎登陸借書系統(tǒng)");
		Library?lb=new?Library();
		lb.library();

	}

}


正在回答

3 回答

foreach操作數(shù)組語(yǔ)法:

foreach(元素類型 ?元素變量: 遍歷對(duì)象){
執(zhí)行代碼 ? ? ? ? ?
}

我覺得是是你沒有搞清楚這個(gè)概念問題,另外我感覺你的程序?qū)懙牟诲e(cuò)!贊

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

極客小白 提問者

非常感謝!
2016-05-31 回復(fù) 有任何疑惑可以回復(fù)我~

神馬?請(qǐng)教詳解

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

與變量的作用域有關(guān)?

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

舉報(bào)

0/150
提交
取消

異常處理之借書系統(tǒng)編寫,為什么用foreach()遍歷引用類型數(shù)組,遍歷對(duì)象一欄也就是數(shù)組名books會(huì)報(bào)錯(cuò)?但是用for()語(yǔ)句來遍歷數(shù)組books就不會(huì)報(bào)錯(cuò),且數(shù)組前必須加public static?求大神解,在線等急。

我要回答 關(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)