我想根据他们的分数对一些球队进行排序(胜利:3分,平局:1分,失败:0分)。我有一个数组,其中包含一些球队的名字和他们的结果,我已经提取他们的分数然后按降序排序。这是我使用的代码:
import java.util.*;
import java.util.Map.Entry;
public class SCAN{
public static void main(String[] args) {
Map<String, Integer> points = new HashMap<>();
String[] challenges = new String[]{
"Team(A) 2 - 1 Team(C)",
"Team(D) 3 - 1 Team(A)",
"Team(B) 3 - 3 Team(D)",
"Team(C) 0 - 4 Team(B)",
"Team(B) 2 - 0 Team(A)"
};
calcualte(challenges, points);
List<Entry<String, Integer>> entries = new ArrayList<Entry<String, Integer>>(
points.entrySet());
Collections.sort(entries, new Comparator<Entry<String, Integer>>() {
public int compare(Entry<String, Integer> e1,
Entry<String, Integer> e2) {
return e2.getValue().compareTo(e1.getValue()); // Sorts
// descending.
}
});
Map<String, Integer> orderedMap = new LinkedHashMap<String, Integer>();
for (Entry<String, Integer> entry : entries) {
orderedMap.put(entry.getKey(), entry.getValue());
}
for (Entry<String, Integer> element : entries) {
System.out.println(element);
}
}
private static void calcualte(String[] challenges, Map<String, Integer> points) {
List<String> challengeList = new ArrayList<>();
for (String str : challenges) {
String[] bits = str.trim().split(" ");
String firstTeam = bits[0];
String lastTeam = bits[bits.length - 1];
if (!challengeList.contains(firstTeam)) {
challengeList.add(firstTeam);
}
if (!challengeList.contains(lastTeam)) {
challengeList.add(lastTeam);
}
int firstScore = Integer.parseInt(bits[1]);
int lastScore = Integer.parseInt(bits[3]);
if (firstScore > lastScore) {
insert(3, points, firstTeam);
insert(0, points, lastTeam);
} else if (firstScore < lastScore) {
insert(3, points, lastTeam);
insert(0, points, firstTeam);
} else {
insert(1, points, firstTeam);
insert(1, points, lastTeam);
}
{
}
}
}
private static void insert(int i, Map<String, Integer> points, String team) {
int score = points.containsKey(team) ? points.get(team) : 0;
score += i;
points.put(team, score);
}
}
我知道这一点,但我应该通过使用超类来实现它所以我需要一个名为Plays
的子类,并且应该从名为Playclass
的超类扩展。 Playclass
不应更改。这个类需要三个抽象的方法,它们应该被覆盖,它们是won()
,drawn()
和defeated()
。另一个应该被覆盖的方法是Print()
,它显示每个团队的信息,如下:
public abstract class Playclass {
private String name;
Playclass(String name){
this.name=name;
}
public String getName() {
return name;
}
public abstract void print();
public abstract void won();
public abstract void drawn();
public abstract void defeated();
}
主要的类名可以是Playleage
,它应该包含我在第一个代码中显示的String[] challenges = new String[]{…}
。我知道如何根据他们的分数(如第一个代码)对它们进行排序,但我不知道如何使用我所说的类。愿有人帮帮我吗?感谢