用java编写小程序,已知a、b、c 3个队伍进行足球比赛,胜利3记分,平局1分,输0分
比赛信息存放在数组中String[]str={"a-b1:1","a-c2:1","b-c0:1"},输出积分和进球数;并由高到低对3个队伍进行排名...
比赛信息存放在数组中 String[] str={ "a-b 1:1" ,"a-c 2:1","b-c 0:1"} ,输出积分和进球数;并由高到低对3个队伍进行排名
展开
1个回答
展开全部
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
final String[] str = { "a-b 1:1", "a-c 2:1", "b-c 0:1" };
final Map<String, Integer> map = new HashMap<String, Integer>();
for (String s : str) {
final String[] temp = s.split(" ");
final String[] names = temp[0].split("-");
final String[] counts = temp[1].split(":");
for (int i = 0; i < 2; i++) {
String name = names[i];
int count = Integer.parseInt(counts[i]);
if (map.containsKey(name)) {
map.put(name, map.get(name) + count);
} else {
map.put(name, count);
}
}
}
final List<Team> teams = new LinkedList<Team>();
for (String name : map.keySet()) {
teams.add(new Team(name, map.get(name)));
}
Collections.sort(teams);
System.out.println("队伍 : 分");
for (Team t : teams) {
System.out.println(t.getName() + " : " + t.getCount());
}
}
}
class Team implements Comparable<Team> {
private String name;
private int count;
public Team(String name, int count) {
this.name = name;
this.count = count;
}
public void addCount(int count) {
this.count = this.count + count;
}
public String getName() {
return name;
}
public int getCount() {
return count;
}
public int compareTo(Team o) {
return o.getCount() - this.count;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询