0
public class dog {
String name;
public void bark()
{
System.out.println(name+ "says warf!");
}
public static void main(String[] args) {
dog dog1 = new dog();
dog1.bark();
dog1.name="bart";
//creation of array
dog[] dogs= new dog[3];
//object reference
dogs[0]= new dog();
dogs[1]= new dog();
dogs[2]= dog1;
//Accessing object variables
dogs[0].name= "fred";
dogs[1].name= "marge";
System.out.println("last dog's name is");
System.out.println(dogs[2].name);
//looping through array
int x=0;
while(x < (dogs.length));
{
dogs[x].bark();
x=x+1;
}
}
大家好.... 我是新來的Java,並在abouve代碼已經在新秀Java編程.... ....按照「頭第一個Java「教科書... 輸出應該是
"null says warf!
last dog's name is bart
fred says warf!
marge says warf!"
,但在上面的代碼,因爲我編碼它在Eclipse IDE中.....輸出m如果前兩行,但不是最後兩.. ..似乎while循環沒有得到執行..... 可以anybode告訴我最新的代碼引用輸出的問題?