2013-01-20 147 views
0

我正在構建一個模擬潛水比賽的程序。我有一個類Athlete,實質上是試圖訪問另一個類中的數組元素。有誰會知道我如何訪問數據?將對象添加到對象數組中

這裏是Athlete類:

public class Athlete { 

    public static String[] name = { "Art Class", "Dan Druff", "Jen Tull" }; 
    public static String[] country = { "Canada", "Germany", "USA" }; 
    Performance[] performance = new Performance[2]; 

    public Athlete(String[] name, String[] country) { 
     this.name = name; 
     this.country = country; 
     // this.event = event; 
    } 

    public void perform() { 
     Dive mydive = new Dive(Dive.diveName, Dive.difficulty); 
     Performance event = new Performance(event.dive, Performance.judgeScores); 
     performance[0] = event(event.dive.diveNames[0], event.judgeScores); // here 
     performance[1] = event; // Here 
     performance[2] = event; // Here 
    } 

    public void printResults() { 
    } 
} 

其中以「//這裏的」我試圖從如下所示的類訪問數據表示,我不知道我該怎麼做?

性能:

public class Performance { 

    Dive dive = new Dive(Dive.diveName, Dive.difficulty); 
    public static float[] judgeScores = new float[7]; 

    public Performance(Dive dive, float[] judgeScores) { 
     this.dive = dive; 
     // this.dive=difficulty; 
     this.judgeScores = judgeScores; 
     // this.judgeScores = judgeScores; 
    } 
} 

潛水:

import java.util.Random; 

public class Dive { 
    public static String diveName; 
    public static int difficulty; 

    public static final String[] diveNames = { "reverse pike", "forward pike", 
      "reverse armstand with double somersault", "reverse triple twist", 
      "double forward with triple somersault", "cannon ball" }; 
    public static final int[] diveDifficulties = { 3, 2, 2, 4, 4, 1 }; 

    public static Dive chosenRandomly() { 
     Random rand = new Random(); 
     int num = rand.nextInt(6) + 1; 
     String diveName = diveNames[num]; 
     int difficulty = diveDifficulties[num]; 

     Dive dive = new Dive(diveName, difficulty); 
     return dive; 
    } 

    public Dive(String diveName, int difficulty) { 
     this.diveName = diveName; 
     this.difficulty = difficulty; 
    } 
} 

是我的構造都錯了?

PS:這是做作業,因此有些人可能無法找到我的方法是解決這個問題的適當途徑,但請記住,我在下面的說明。

在此先感謝您的任何意見!

+1

如果您使用的是IDE,可以自動格式化你的代碼,我建議你重新發布代碼的格式,以提高你的問題的可讀性。 – giampaolo

回答

1

假設您正在名爲Javadir的目錄中工作,並且您創建了四個文件,其內容如下所示。

文件1

package ListPkg; 
public class List { ... } 
class ListNode {...} 

文件2

package ListPkg; 
public class NoNextItemException { ... } 

文件3

public class Test { ... } 
class Utils { ... } 

文件4

class Test2 { ... } 

以下是您必須使用的目錄和文件名:

文件1必須位於名爲ListPkg的子目錄中,文件名爲List.java

文件2還必須位於名爲NoNextItemException.java的文件中的ListPkg子目錄中。

文件3必須位於名爲Test.java(位於Javadir目錄中)的文件中。

文件4可以在任何.java文件中(在Javadir目錄中)。

更多的指導方針,通過這個notes on java packages

+0

它們位於相同的文件夾 – choloboy

+0

@theolc按照上面的示例解決您的問題 –

1

通常這是不是一個好主意來訪問從其他類(甚至公共)的字段。您應該爲您的數組字段創建getters並訪問它們。