gpt4 book ai didi

java - 满足条件时增加map值

转载 作者:行者123 更新时间:2023-11-30 05:40:25 25 4
gpt4 key购买 nike

我遇到了困难,修订书没有太大帮助,因为它提供了简单的示例,我的修订作业要求我声明一个类型的局部变量来引用列表,Teams,然后如果应该获取列表球队的键值划分,并将其分配给局部变量。

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

我已经设法编写了迭代列表的方法和 if-else 语句来使用有效的 .get() 方法检查给定的参数

我遇到的问题是访问 Team 类中的 incWon() 方法来更改每个 map 值的 win 值。尽管该 Material 给出了如何更改 map 值的非常简单的示例,但并没有真正解释我如何使用动态输入更改值。

任何帮助将不胜感激,因为如果我能赢得工作,其余的就会水到渠成。

这是我的课

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


}

最佳答案

我认为您误解了数据结构要求。您正在循环遍历团队列表,并且从不更新循环变量。我相信数据结构应该更像是这样的:

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

使用外部 map 键的分区,以及内部 map 键的团队名称。这将div1分开的teamA来自div2teamA

这简化了您的 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 - 满足条件时增加map值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55763500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com