Java 满足条件时增加映射值

Java 满足条件时增加映射值,java,maps,Java,Maps,我遇到了麻烦,修订本没有多大帮助,因为它提供了简单的示例,我的修订分配要求我声明一个引用列表Teams类型的局部变量,然后if应该获取键值划分的团队列表,并将其分配给局部变量 然后,如果a队的得分高于B队,则要求我增加a队的获胜次数,反之亦然,如果是平局,则增加平局次数 我已经成功地编写了迭代列表的方法,以及使用.get()方法检查给定参数的if-else语句 我遇到的问题是访问Team类中的incWon()方法来更改每个map值的won值。不过,该材料给出了如何更改贴图值的简单示例,但并没有真

我遇到了麻烦,修订本没有多大帮助,因为它提供了简单的示例,我的修订分配要求我声明一个引用列表Teams类型的局部变量,然后if应该获取键值划分的团队列表,并将其分配给局部变量

然后,如果a队的得分高于B队,则要求我增加a队的获胜次数,反之亦然,如果是平局,则增加平局次数

我已经成功地编写了迭代列表的方法,以及使用.get()方法检查给定参数的if-else语句

我遇到的问题是访问Team类中的incWon()方法来更改每个map值的won值。不过,该材料给出了如何更改贴图值的简单示例,但并没有真正解释如何使用动态输入更改值

任何帮助都将不胜感激,因为如果我能赢得工作,其余的都会到位

这是我的课

{
   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));
      }
   }
}

我认为您误解了数据结构要求。您在一个团队列表中循环,并且从不更新循环变量。我认为数据结构应该更像这样:

私人地图小组

使用分区作为外部映射键,使用团队名称作为内部映射键。这将
div1
teamA
div2
teamA

这简化了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();
      }
   }
public void recordResult(字符串分割、字符串teamA、字符串teamB、int teamAScore、int teamBScore)
{
Map teamKeys=teams.get(部门);
如果(teamAScore>teamBScore)
{
teams.get(teamA.incWon();//更新teamA的胜利
}
else if(teamAScore
在recordResult(字符串分割,字符串teamA,字符串teamB,int teamAScore,int teamBScore)中,什么是分割,什么是teamA,teamB字符串..它是团队的名称?我想:
teams.get(teamB);//teamB的更新赢家实际上应该是:
teams.get(division)
来获取团队集合,然后您需要一个迭代器来获取您想要的团队,并对其应用该方法。查看此帖子:我们收到的测试代码是leagueA.recordResult(“英超”、“阿森纳”、“切尔西”2,1)
作为字符串的值。谢谢您的帮助,但是当我在现有类中编译上面的代码时,在addTeam()方法中会出现类型不兼容的编译器错误。。特别是
this.teams.put(部门,团队集)
在这个方法中我需要更改什么?团队集需要是一个映射,而不是一个集合。基本上,您将拥有一个部门到团队地图的映射,这将把团队名称映射到团队对象。
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();
      }
   }