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

然后要求我增加teamA的获胜次数(如果得分高于teamB,反之亦然),如果是平局,则增加平局次数。

我设法编写了一种方法来遍历列表和一个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());
   }


}

最佳答案

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

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

对外部地图键使用分区,对内部地图键使用团队名称。这将div1teamAdiv2teamA分开

这简化了您的recordResult方法来吸引特定团队并适当地更新他们

public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
   {
      Map<String, Team> teamKeys = teams.get(division);
      if(teamAScore > teamBScore)
      {
         teams.get(teamA).incWon(); // updates wins for teamA
      }
      else if (teamAScore < teamBScore)
      {
         teams.get(teamB).incWon(); // updates wins for teamB
      }
      else
      {
         teams.get(teamA).incDrew();
         teams.get(teamB).incDrew();
      }
   }

关于java - 满足条件时的增量图值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55763500/

10-12 00:06