我有一种打印出运动员比赛结果的方法。此方法打印输入类别的最高分数。问题是它打印出每个运动员的每一个结果。我只希望打印出每位运动员输入类别的最高价值。我已经远远地搜索了一个答案但却无法知道如何用我有限的知识来做到这一点。所以我的问题是:如何只打印出1名运动员的最高价值,这样我才能获得真正的记分牌?
void typeOutHighScores() {
ArrayList<Athlete> athletes= AthleteList.getArrayList();
Collections.sort(resultlist);
String categoryToShow = null;
System.out.println("Which category would you like to show highscores for?");
categoryToShow = scanner.nextLine();
categoryToShow = normalize(categoryToShow); //Just makes all letters small with first letter as capital.
category matchedCategory= null;
ArrayList<category> categories = CategoryList.getArrayList();
for (int i = 0; i < categories.size(); i++) {
category c = categories .get(i);
if (c.categoryName().equals(categoryToShow)) {
matchedCategory= c;
break;
}
}
if (matchedCategory== null) {
System.out.println("Couldn't find " + categoryToShow + ".");
} else {
System.out.println("Resultatlist for " + categoryToShow + ": ");
for (int i = 0; i < resultlist.size(); i++) {
Athlete matched = null;
int order = 0;
Result res = resultlist.get(i);
if (res.categoryName().equals(categoryToShow)) {
for (int x = 0; x < athletes.size(); x++) {
Athlete del = athletes.get(x);
if (res.athleteStartNumber() == del.startNumber()) {
matched = del;
order = i + 1;
System.out.println(order + ". " + matched.surName() + " " + matched.lastName()
+ " has the result: " + res.categoryValue());
break;
}
}
}
}
}
}
答案 0 :(得分:1)
考虑一下:
Map<Athlete,Double> athleteResults = new HashMap<Athlete,Double>();
而不是打印每个运动员
if(!athleteResults.containsKey(matched) //no score yet
|| athleteResult.get(matched) < res.categoryValue()) { //higher score
athleteResult.put(matched, res.categoryValue());
}
然后迭代地图并打印结果。
祝你好运。