如何比较Java中两个对象的参数值?

时间:2015-03-03 23:13:58

标签: java object

现在我正在研究一种比较奥运会运动员得分的方法。到目前为止,我没有遇到什么麻烦,但是现在我已经达到了我需要比较两个物体(运动员)得分的程度,而且我不知道该怎么做。这是我的奥运会代码:

// A program using the Athlete class
public class Olympics {
 public static void main(String args[]) {
 System.out.println("The leader is " + Athlete.leader() +
 ", with a score of " + Athlete.leadingScore());

 Athlete meryl = new Athlete("Meryl Davis", "U.S.");
 meryl.addScore(75);
 System.out.println(meryl);

 Athlete tessa = new Athlete("Tessa Virtue", "Canada");
 System.out.println(tessa);
 System.out.println(); // blank line

 tessa.addScore(50);
 System.out.println(tessa);
 System.out.println(meryl);
 System.out.println("The leader is " + Athlete.leader() +
 ", with a score of " + Athlete.leadingScore());
 System.out.println(); // blank line

 tessa.addScore(100);
 meryl.addScore(65);
 System.out.println(tessa);
 System.out.println(meryl);
 System.out.println("The leader is " + Athlete.leader() +
 ", with a score of " + Athlete.leadingScore());
 System.out.println(); // blank line

 tessa.addScore(20);
 System.out.println("Tessa's final score is " + tessa.getScore());

 meryl.move("France");
 System.out.println(meryl);
 } // end main
} // end class Olympics

这是构造者类"运动员":

public class Athlete {
    private String name;
    private String country;
    protected int score;
    public static int leadScore;
    public Athlete(String athName, String athCountry) {
        this.name = athName;
        this.country = athCountry;
        score = 0;
        if (score < 1) {
            System.out.println("Score cannot be lower than 1");
        }
    }
    public int addScore(int athScore) {
        score += athScore;
        return score;
    }
    public static String leader(){
        //TODO
    }
    public static int leadingScore() {
       //MUST COMPARE BOTH ATHLETES
    }
    public int getScore(){
        return score;
    }
    public void move(String newCountry) {
        country = newCountry;
    }
    public String toString() {
        return name + ": " + "from " + country + ", current score " + score;
    }
}

所以我要做的就是让程序检查Meryl的得分与Tessa相比,并将该运动员的得分归结为leadingScore(),并使用该运动员,返回领导者()。任何帮助表示赞赏!感谢。

3 个答案:

答案 0 :(得分:0)

该功能必须将您正在比较的两名运动员作为此项工作的参数

public static int leadingScore(Athlete a1, Athlete a2) {
    if (a1.getScore() < a2.getScore()) {
        // do stuff
    }
}

答案 1 :(得分:0)

您应该考虑使用"collection"。使用数组,列表......甚至是排序列表。

将您的单个对象存储在集合中,然后遍历该集合以找到最高分。

例如:

// Create athlete objects; add each to list
ArrayList<Athlete> athletes = new ArrayList<Athlete>(); 
Athlete meryl = new Athlete("Meryl Davis", "U.S.");
meryl.addScore(75);
...
athletes.add(meryl);

Athlete tessa = new Athlete("Tessa Virtue", "Canada");
...
athletes.add(tessa );

// Go through the list and find the high scorer
Athlete highScorer = ...;
for (Athlete a : athletes) {
  if (highScorer.getScore() < a.getScore())
    highScorer = a;
  ...
}
System.out.println("High score=" + highScorer.getScore());

这是一个很好的教程:

答案 2 :(得分:0)

主要分数不应该在运动员类中,而应该在main()中,因为运动员类的一个实例不会知道其他实例,除非您在课程中放入自引用列表。同样,leadingScore应该在main()。

它或者主要可以给每个运动员打电话并进行比较:

int merylScore = meryl.getScore ();
int tessaScore = tessa.getScore ();
int leadingScore = 0;
String leaderName = "";
if (merylScore > tessaScore) {
    leadingScore = merylScore;
    leaderName = meryl.getName ();
} else if (tessaScore > merylScore) {
    leadingScore = tessaScore;
    leaderName = tessa.getName ();
} else {
    leadingScore = merylScore;
    leaderName = "a tie between Meryl and Tessa";
}
System.out.println ("The leader is " + leaderName + ", with a score of " + leadingScore);