简体   繁体   English

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

[英]How to compare parameter values of two objects in Java?

Right now I'm working on a method for comparing the scores of athletes in the olympics. 现在我正在研究一种比较奥运会运动员得分的方法。 So far I've had little trouble, however now I've reached a point where i need to compare two objects (athletes) scores and I'm not sure how to do it. 到目前为止,我遇到了一些麻烦,但是现在我已经达到了需要比较两个物体(运动员)得分的程度,我不知道该怎么做。 This is my code for the Olympic class: 这是我的奥运课程代码:

// 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

And this is the constructor class "Athlete": 这是构造类“运动员”:

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;
    }
}

So what I'm trying to do is have the program check Meryl's score compared to Tessa's and return that Athlete's score in leadingScore() and, using that athlete, return a leader(). 所以我要做的是让程序检查Meryl的得分与Tessa相比,并返回运动员在leadingScore()中的得分,并使用该运动员返回领导者()。 Any help is appreciated! 任何帮助表示赞赏! Thanks. 谢谢。

The function must take the two Athletes you're comparing as the parameters for this to work 该功能必须将您正在比较的两名运动员作为此项工作的参数

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

You should consider using a "collection" . 你应该考虑使用“集合” Use an array, a list ... or even a sorted list. 使用数组,列表......甚至是排序列表。

Stored your individual objects in the collection, then traverse the collection to find the highest score. 在集合中存储您的单个对象,然后遍历集合以查找最高分。

For example: 例如:

// 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());

Here's a good tutorial: 这是一个很好的教程:

The lead score should not be in the athlete class, but rather in main () because one instance of an Athlete class would not know of other instances unless you put a self-referential list inside the class. 主要分数不应该在运动员类中,而应该在main()中,因为运动员类的一个实例不会知道其他实例,除非您在类中放入自引用列表。 Similarly, leadingScore should be in main (). 同样,leadingScore应该在main()中。

It or main can call each athlete and compare: 它或主要可以打电话给每个运动员并比较:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM