【问题标题】:adding extra to a Map向地图添加额外内容
【发布时间】:2021-06-16 09:55:49
【问题描述】:

您好,我正在尝试重新创建足球锦标赛,并且能够使用地图创建组,但现在想将积分添加到每个国家/地区。有人有什么好主意吗?请看看我已经做了什么。

import java.util.*;

class EuroGroupStages {
    
    public static void main(String args[]) {

         Map<String, Set<String>> groupA;
         
//public EuroGroupStages() 
//  {
         groupA = new TreeMap<>();

//  }
         
//public void addCountries(String aGroup)
//  {
        Scanner keyboard = new Scanner(System.in);
        String aCountry, aGroup;
        char anAnswer;
        boolean flag = true;
        
        while(flag)
        {
            Set<String> country = new HashSet<>();
            
            System.out.print("Please enter the group name (A-D):");     
            aGroup = keyboard.next();
        
                for(int i = 1; i < 5; i++)
                {
                    System.out.print("Please enter a country");
                    aCountry = keyboard.next();
                    country.add(aCountry);
                    
                }

            System.out.print("Do you want to continue? Y/N");
            anAnswer = keyboard.findWithinHorizon(".", 0).charAt(0);
                
                if(anAnswer == 'N' || anAnswer == 'n')
                {
                    flag = false;
                }
            groupA.put(aGroup, country);
        }

        System.out.println(groupA);
        
        
        keyboard.close();
//  }

    }
}

【问题讨论】:

    标签: java class methods collections maps


    【解决方案1】:

    我猜你最好创建Group 和Team 类。然后每个Group 将包含Set/List/...(最适合的)Team 对象。

    在Team 类中,您可以提供一个包含该团队在小组赛阶段得分的属性。

    为您的Group:

    public class Group {
        private String name;
        private Set<Team> teams;
    
        public Group(String name) {
            this.name = name;
            this.teams = new TreeSet<>();
        }
    
        public void addTeam(Team team) {
            teams.add(team);
        }
    }
    

    为您的Team:

    public class Team {
        private String name;
        private int points;
    
        public Team(String name) {
            this.name = name;
        }
    
        public void addPoints(int amount) {
            this.points += amount;
        }
    }
    

    使用示例:

    public class EuroGroupStages {
        public static void main(String[] args) {
            // Create group B
            Group groupB = new Group("B");
    
            // Create teams in group B
            Team belgium = new Team("Belgium");
            Team finland = new Team("Finland");
            Team denmark = new Team("Denmark");
            Team russia = new Team("Russia");
    
            // Add teams of group B to group B
            groupB.addTeam(belgium);
            groupB.addTeam(finland);
            groupB.addTeam(denmark);
            groupB.addTeam(russia);
    
            // First matches were played, Belgium and Finland won
            belgium.addPoints(3);
            finland.addPoints(3);
        }
    }
    

    注意:我没有运行此代码,它是即时编写的,但应该能让您了解如何解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 2018-07-22
      • 2016-11-22
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多