/* Assume as precondition that the list of players is not empty.
* Returns the winning score, that is, the lowest total score.
* @return winning score
*/
public int winningScore() {
Player thePlayer = players.get(0);
int result = thePlayer.totalScore();
for (int i = 0; i < players.size(); i++){
int p = players.get(i).totalScore();
if (p < result) {
result = players.get(i).totalScore();
}
}
return result;
}
/* Returns the list of winners, that is, the names of those players
* with the lowest total score.
* The winners' names should be stored in the same order as they occur
* in the tournament list.
* If there are no players, return empty list.
* @return list of winners' names
*/
public ArrayList<String> winners() {
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < players.size(); i++)
if (!players.isEmpty())
return result;
}
正如评论中所述,我试图在 Winners 方法中返回 WinningScore() 结果,以便它返回获胜者/获胜者姓名。
我设法只返回所有获胜者,但我有点困惑是否应该从 WinningScore() 方法调用?
我了解我当前的代码对于获奖者来说不正确
任何正确方向的推动/提示将不胜感激!谢谢!
您想要做的是在 Winners 方法中找到所有具有获胜分数的玩家对象。
- 为此,您需要首先通过调用计算获胜分数
你的winingScore 方法。
- 接下来你找到所有totalScore等于的玩家对象
先前计算的获胜分数。你想归还那些。
获胜者方法的生成代码将如下所示:
public ArrayList<String> winners() {
ArrayList<String> result = new ArrayList<String>();
int winningScore = winningScore();
for (int i = 0; i < players.size(); i++)
if (players.get(i).totalScore() == winningScore)
result.add(players.get(i).getName())
return result;
}
如果你想简化代码,你可以替换for
循环使用ArrayList
像这样的迭代器,因为您不使用索引变量i
:
for (Player player : players) {
if (player.totalScore() == winningScore)
result.add(player.getName())
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)