如何创建一个记录两个不同团队之间胜负的方法? 这是团队对象&构造
public class NFLTeam4
{ private int win;
private int loss;
private String team;
public NFLTeam4(String teamName)
{ win = 0;
loss = 0;
team = teamName;
}
public NFLTeam4(int w, int l)
{ win = w;
loss = l;
}
}
这是我尝试创造一种胜利的游戏方法。
public class NFLGameDay4{
public static void main(String[] args){
NFLTeam4 falcons = new NFLTeam4("Falcons");
NFLTeam4 chiefs = new NFLTeam4("Chiefs");
NFLTeam4 jets = new NFLTeam4("Jets");
}
public NFLTeam4 winAGame(NFLTeam4 b)
{ int win = 1;
int loss = 0;
return new NFLTeam4(win, loss);
}
答案 0 :(得分:1)
首先,您的NFLTeam4
课程似乎能够代表团队......
public NFLTeam4(String teamName) {
win = 0;
loss = 0;
team = teamName;
}
......或团队的记录......
public NFLTeam4(int w, int l) {
win = w;
loss = l;
}
但不是两者:没有办法预先提供团队名称,然后再修改记录。要纠正这一点(从而使管理团队的记录成为可能),请考虑删除第二个构造函数(int w, int l
)并创建两个方法来处理为您更改记录:
public void addWin() {
++win;
}
//similar code for addLoss
请注意,您可能希望添加getWinCount
之类的方法。您要添加的内容以及编码方式取决于您,这只是为了让您入门。
现在winAGame
只需要在提供的小组addWin
)上调用NFLTeam4 b
。
答案 1 :(得分:0)
您可以在Map
课程中创建结果NFLTeam4
。
public class NFLTeam4
{
private enum RESULT { WIN, LOSS };
private Map<RESULT, List<NFLTeam4>> results = new HashMap<RESULT, ArrayList<NFLTeam4>>;
// you can leave the constructor and other variables as they are
public void winAGame(NFLTeam4 b) {
win = win + 1;
// Now check and add to your results map
// you will need to make sure your map is initialised properly
results.get(RESULT.WIN).add(b);
}
}
您正在封装NFLTeam4对象中赢或输游戏的行为。您还可以扩展RESULT
枚举,以包含有关结果的日期,游戏编号,地点等的一些信息。
这只是一个关于如何运作的说明。您应该根据要求考虑如何设计对象。