中易网

用java编写小程序,已知a、b、c 3个队伍进行足球比赛,胜利3记分,平局1分,输0分

答案:1  悬赏:80  
解决时间 2021-01-18 06:20
  • 提问者网友:饥饿走向夜
  • 2021-01-17 14:03
用java编写小程序,已知a、b、c 3个队伍进行足球比赛,胜利3记分,平局1分,输0分
最佳答案
  • 二级知识专家网友:十鸦
  • 2021-01-17 14:42
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 map = new HashMap();
        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 teams = new LinkedList();
        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 {
    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;
    }
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息