因此,我有一个称为Team的类,用于创建Team对象。在另一个名为LeagueAdmin的类中,我创建了一个map<String, List<Team>
类型的映射,正如您在我的代码中看到的那样,我将键设置为分区字符串,然后将团队对象分配给不同的分区。
我的问题是我试图创建一种方法,该方法在一个部门中查找两个指定的团队,比较两个整数,如果x大于y,则对每个对象调用某个方法。也就是说,如果teamA的进球数比teamB多,则将与teamA获胜值同名的对象增加1。
我的LeageAdmin代码是:
import java.util.*;
public class LeagueAdmin {
private Map<String, List<Team>> teams;
/**
* Constructor for objects of class LeagueAdmin
*
*/
public LeagueAdmin() {
this.teams = new HashMap<>();
}
public void addTeam(String division, Team team) {
if (!this.teams.containsKey(division)) {
List<Team> teamList = new ArrayList<>();
teamList.add(team);
this.teams.put(division, teamList);
} else {
List<Team> newTeam = this.teams.get(division);
newTeam.add(team);
this.teams.put(division, newTeam);
}
}
public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
{
List<Team> teamList = teams.get(division);
for(Team team : teamList) {
if(teamA.equals(team.getName())) {
teamA = String.valueOf(team);
} else if (teamB.equals(team.getName())) {
teamB = String.valueOf(team);
}
}
if (teamAScore == teamBScore)
{
teams.get(division).get(teamA).incDrew();
teams.get(division).get(teamB).incDrew();
} else if (teamAScore > teamBScore) {
teams.get(division).get(teamA).incWon();
teams.get(division).get(teamB).incLost();
} else {
teams.get(division).get(teamA).incLost();
teams.get(division).get(teamB).incWon();
}
}
}
团队的代码为:
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());
}
}
正如您所看到的,方法recordResult()的代码是我遇到的问题,在这里我从方法的参数中引用字符串,似乎认为它应该接收整数?我的代码几乎是正确的还是有更好的方法?
public void recordResult(String division, String teamA, String teamB, int teamAScore, int teamBScore)
{
teamA = null;
teamB = null;
List<Team> teamList = teams.get(division);
for(Team team : teamList) {
if(teamA.equals(team.getName())) {
teamA = String.valueOf(team);
} else if (teamB.equals(team.getName())) {
teamB = String.valueOf(team);
}
}
if (teamAScore == teamBScore)
{
teams.get(division).get(teamA).incDrew();
teams.get(division).get(teamB).incDrew();
} else if (teamAScore > teamBScore) {
teams.get(division).get(teamA).incWon();
teams.get(division).get(teamB).incLost();
} else {
teams.get(division).get(teamA).incLost();
teams.get(division).get(teamB).incWon();
}
}
答案 0 :(得分:0)
您已接近可用的东西! recordResults()
似乎旨在修改Team
对象的状态,但是由于该方法接受组名而不是Team
对象,因此您需要先进行查找。
您的for
循环正在通过适当分隔中的Team
对象查找Team
,其名称由函数自变量teamA
或teamB
中提供。很好,您只需要将Team
对象存储在变量中而不是组名中,这样以后就可以在方法中访问它了。因此,您不想存储String.valueOf(team)
,而是要存储team
。
请稍等。我无法将Team
对象放入String
变量中!你说的没错。方法开头的teamA
和teamB
变量应声明为Team
类型,并具有与方法的参数名称不冲突的名称。就我个人而言,我将重命名方法参数teamAName
和teamBName
,因为它们仅包含组名,而不包含整个Team
对象。
现在您已经成功地按它们的名称查找了Team
对象(我们将在以后的练习中保留错误处理),您只需要调用它们适当的计分方法即可。因此,在检查团队得分的if-else
块中,应直接在我们从查找中保存的inc...
和teamA
变量上调用teamB
方法。