2013-01-20 99 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并访问它们。