满足条件时的增量图值

时间:2019-04-19 14:35:54

标签: java maps

我遇到了麻烦,修订书因为提供了简单的示例而无济于事,我的修订工作要求我声明一个局部变量,以引用一个列表(团队),然后应该获取列表个团队进行键值划分,并将其分配给局部变量。

然后问我,如果得分高于团队B,则增加teamA的获胜次数,反之亦然;如果是平局,则增加平局次数。

我设法编写了一种遍历列表的方法,并编写了一个if-else语句,以使用有效的.get()方法检查给定的参数

我遇到的问题是在Team类的incWon()方法中访问以更改每个地图值的韩元值。尽管该材料给出了关于如何更改地图值的相当平坦的示例,但并未真正说明我如何使用动态输入来更改值。

任何帮助将不胜感激,好像我可以赢得工作,其余的将落到位。

这是我的课程

{
   private SortedMap<String, Set<Team>> teams;
   /**
    * Constructor for objects of class LeagueAdmin
    */
   public LeagueAdmin()
   {
      // Create the HashMap
      //Map<String, Team> teams = new HashMap<>();
      super();
      this.teams = new TreeMap<>();

   }

   public void addTeam(String division, Team team)
   {
      boolean changed;

      if (!this.teams.containsKey(division)) // checks if the key division doesn't contain the value of divsioin  
      {
         HashSet<Team> teamSet = new HashSet<>(); // instantiates a list of objects called Team and assigns them to local variable teamSet
         teamSet.add(team); // adds a new team to the list

         this.teams.put(division, teamSet);
         changed = true;
      }
      else
      {
         Set<Team> teamSet = this.teams.get(division); // if list exists already adds new team to existing list
         changed = teamSet.add(team);
      }

   }     

   public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
   {
      Set<String> teamKeys = teams.keySet();
      for (String eachDivision: teamKeys)
      {
         if(teamAScore > teamBScore)
         {
            teams.put(); // updates wins for teamA
            // System.out.println(teamA);
         }
         else if (teamAScore < teamBScore)
         {
            teams.get(teamB); // updates wins for teamB
            // System.out.println(teamB);
         }
         else
         {
            // teams.put(); //updates draws for both teams
         }

         // System.out.println(eachDivision + " teams are " + teams.get(eachDivision));
      }
   }
}

这是我必须访问的TEAM类的增量值。

public class Team
{  
   private String name;
   private String division;
   private int won;
   private int drew;
   private int lost;
// no need to record points as = 3*won + drew   


   /**
    * Constructor for objects of class Team
    */
   public Team(String aName, String aDivision)
   {
      name = aName;
      division = aDivision;
      // no need to set won, drew and lost to 0
   }


   /**
    * getter for attribute points
    */
   public int getPoints()
   {
      return 3 * won + drew;
   }

   /**
    * getter for name
    */
   public String getName()
   {
      return name;
   }

   /**
    * getter for division
    */
   public String getDivision()
   {
      return division;
   }
   /**
    * getter for won
    */
   public int getWon()
   {
      return won;
   }   

   /**
    * getter for drew
    */
   public int getDrew()
   {
      return drew;
   }

   /**
    * getter for lost
    */
   public int getLost()
   {
      return lost;
   }   

   /**
    * increments the number of games won
    */   
   public void incWon()
   {
      won = won + 1;
   }

   /**
    * increments the number of games drawn
    */    
   public void incDrew()
   {
      drew = drew + 1;
   }

   /**
    * increments the number of games lost
    */    
   public void incLost()
   {
      lost = lost + 1;
   }

   /**
    * setter for division
    */
   public void setDivision(String aDivision)
   {
      division = aDivision;
   }

   public String toString()
   {
      return ("Team " + name + ", division: " + division + " stats: Won: " + won
       + ", drew: " + drew + ", lost: " + lost + ", points: " + getPoints());
   }


}

1 个答案:

答案 0 :(得分:1)

我认为您误解了数据结构要求。您正在遍历团队列表,而从未更新loop变量。我认为数据结构应该更像这样:

private Map<String, Map<String, Team>> teams;

使用外部映射键的分区,以及内部映射键的团队名称。这会将div1的{​​{1}}与teamA的{​​{1}}

这简化了您的recordResult方法来吸引特定团队并相应地对其进行更新

div2