对于Java和一般编码来说还不错。我创建了一个程序,该程序读取包含几个足球比赛结果的.txt文件,然后用于输出一个记分牌,显示按数字顺序排列的得分,然后按字母顺序显示并列得分。到目前为止,我已经获得了输出,但是似乎无法按值对结果进行排序。任何帮助都将是惊人的。
public class Scoreboard {
private static class Results {
public int score = 0;
public void addWin() {
score += 3;
}
public void addTie() {
score += 1;
}
public void addlost() {
}
@Override
public String toString() {
return " " + score;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Jeremy\\Desktop\\ADV JAVA\\SoccerProject\\SoccerScoresTest.txt"));
String line;
Map<String, Results> teamResults = new HashMap<>();
while ((line = br.readLine()) != null) {
String[] data = line.split(" ");
Results team1 = teamResults.getOrDefault(data[0], new Results());
Results team2 = teamResults.getOrDefault(data[2], new Results());
if (Integer.valueOf(data[1]) > Integer.valueOf(data[3])) {
team1.addWin();
team2.addlost();
} else if (Objects.equals(Integer.valueOf(data[1]), Integer.valueOf(data[3]))) {
team1.addTie();
team2.addTie();
} else {
team1.addlost();
team2.addWin();
}
teamResults.put(data[0], team1);
teamResults.put(data[2], team2);
}
Formatter formatter = new Formatter();
System.out.println(formatter.format("%20s %20s", "Team", "Score"));
for (String team : teamResults.keySet()) {
Results teamres = teamResults.get(team);
formatter = new Formatter();
System.out.println(formatter.format("%20s %20s", team, teamres));
}
}
}
当前结果:
Team Score
Liverpool 1
Tarantulas 6
FCAwesome 1
Lions 4
Grouches 0
ManchesterUnited 1
预期结果:
Team Score
Tarantulas 6
Lions 4
FCAwesome 1
Liverpool 1
ManchesterUnited 1
Grouches 0