[Java]請幫我看哪寫錯了

2007-07-24 4:19 am
請問一下,我要印出Apple has been planted,Apple is growing,...,Strawberry has been harvested;該修改哪裡??

class Apple{
private int treeAge;
public void plant(){
System.out.println("Apple has been planted");
}
public void grow(){
System.out.println("Apple is growing");
}
public void harvest(){
System.out.println("Apple has been harvested");
}
public int getTreeAge(){
return this.treeAge ;
}
public void setTreeAge (int treeAge) {
this.treeAge= treeAge;
}
}
class Grape{
private boolean seedless;
public void plant(){
System.out.println("Grape has been planted");
}
public void grow(){
System.out.println("Grape is growing");
}
public void harvest(){
System.out.println("Grape has been harvested");
}
public boolean getseedless(){
return this.seedless;
}
public void setseedless(boolean seedless){
this.seedless = seedless;
}
}
class Strawberry{
public void plant(){
System.out.println("Strawberry has been planted");
}
public void grow(){
System.out.println("Strawberry is growing");
}
public void harvest(){
System.out.println("Strawberry has been harvested");
}
}
public class java{
void main(){
Apple apple = new Apple();
Grape grape = new Grape();
Strawberry strawberry = new Strawberry();
}
}

回答 (3)

2007-07-24 6:08 am
✔ 最佳答案
public class Java{
public static void main(String[] args){
Apple apple = new Apple();
Grape grape = new Grape();
Strawberry strawberry = new Strawberry();
apple.plant();
apple.grow();
apple.harvest();
grape.plant();
grape.grow();
grape.harvest();
strawberry.plant();
strawberry.grow();
strawberry.harvest();
}
}

紅色為修改部分,其它不變,即可達到你敘述的結果。
希望這樣有幫到你

豚仔
參考: 我
2007-07-24 6:50 am
Java 程式,一個 java 檔案可以包含數個類別,唯一的限制是只能包含一個 public 類別。
2007-07-24 5:37 am
Java 程式…,一個類別只能存成一個 java 檔案。


收錄日期: 2021-04-27 17:16:12
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070723000010KK10488

檢視 Wayback Machine 備份