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

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

這段代碼有什么問(wèn)題呢?

public?class?Book?{
	String?title;
	String?author;
	class?BooksTestDrive?{
	}
	public?static?void?main(String?[]?args)?{
		Book?[]?myBooks?=?new?Book[3];
		int?x?=?0;
		myBooks[0].title?=?"The?Grapes?of?Java";
		myBooks[1].title?=?"The?Java?Gatsby";
		myBooks[2].title?=?"The?Java?Cookbook";
		myBooks[0].author?=?"bob";
		myBooks[1].author?=?"sue";
		myBooks[2].author?=?"ian";
		while?(x?<?3)?{
		System.out.print(myBooks[x].title);
		System.out.print(?"by"?);
		System.out.println(myBooks[x].author);
		x?=?x?+?1;
		}
	}
	}

為什么沒有提示錯(cuò)誤,運(yùn)行的時(shí)候卻有問(wèn)題呢?

正在回答

1 回答

對(duì)數(shù)組的初始化工作沒有結(jié)束,在Java中對(duì)非基本數(shù)據(jù)初始化時(shí),必須使用new。在使用new創(chuàng)建數(shù)組后,此時(shí)數(shù)組還是一個(gè)引用數(shù)組。只有再創(chuàng)建新的對(duì)象,并把對(duì)象賦值給數(shù)組引用,到此初始化結(jié)束。

可參考JAVA對(duì)象數(shù)組的初始化方法


public class Book {

? ? String title;

? ? String author;

? ? public static void main(String [] args) {

? ? ? ?Book[] myBooks = new Book[3];

? ? ??

? ? ? ?myBooks[0] = new Book();

? ? ? ?myBooks[1] = new Book();

? ? ? ?myBooks[2] =new Book();

? ? ? ?

? ? ? ? myBooks[0].title = "The Grapes of Java";

? ? ? ? myBooks[1].title = "The Java Gatsby";

? ? ? ? myBooks[2].title = "The Java Cookbook";

? ? ? ? myBooks[0].author = "bob";

? ? ? ? myBooks[1].author = "sue";

? ? ? ? myBooks[2].author = "ian";

? ? ? ? for(int x = 0; x < 3; x++ ){

? ? ? ? System.out.print(myBooks[x].title);

? ? ? ? System.out.print( " by " );

? ? ? ? System.out.println(myBooks[x].author);

? ? ? ? }

? ? }

? ? }


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

差不多先生1234 提問(wèn)者

也就是說(shuō)我沒有創(chuàng)建對(duì)象是吧?
2016-05-02 回復(fù) 有任何疑惑可以回復(fù)我~
#2

亂也

我試著運(yùn)行了,運(yùn)行不了,代碼錯(cuò)誤
2016-05-19 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

這段代碼有什么問(wèn)題呢?

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

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

幫助反饋 APP下載

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

公眾號(hào)

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