【发布时间】:2019-05-09 03:39:59
【问题描述】:
所以我有一个名为 Team 的类,用于创建 Team 对象。在另一个名为 LeagueAdmin 的类中,我创建了一个 map<String, List<Team> 类型的映射,正如您在我的代码中看到的那样,我将键设置为部门字符串,然后将团队对象分配给不同的部门。
我的问题是我试图创建一个方法,在一个部门中找到两个指定的团队,比较两个整数,如果 x 大于 y,则对每个对象调用某个方法。即如果A队的进球比B队多,则将与A队同名的对象的获胜值加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();
}
}
【问题讨论】:
-
您将
teamA设置为空。为什么?这不仅会在if(teamA.equals(team.getName()))上抛出一个NPE,它还会丢弃teamA的值 -
我已经删除了它,我一定是从我尝试的不同解决方案中留下了它,但是没有那个仍然会出现同样的错误。
-
你得到的确切错误是什么?
-
method get in interface java.util.List<E> cannot be applied to given types; required: int; found: java.lang.String; -
teams.get(division)返回一个List,然后调用.get(teamA),将String传递给get(),它接受一个int 参数。查看indexOf