【发布时间】:2023-03-20 08:56:05
【问题描述】:
我有一个锦标赛课程,我试图通过获得最低分数并从中制作一个数组列表来获得获胜者。我想也许我可以在我的获胜者方法中使用我的 winscore 方法?
这是我的尝试: (但我最终会出错,因为它们不是同一类型)
/**
* 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>();
if (result.isEmpty()) {
return null;
}
result.add(players);
// Supply this code!
return result;
}
我有这个方法,我做了,有什么办法可以把它变成winners方法吗?
/*
* 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();
// Supply this code!
for(int i=0; i <par.length; i++)
if(par[i] > result)
result = par[i];
return result;
}
这是获胜者方法的 Junit 测试:
@Test(timeout=3000)
public void testWinners() {
int [] par = {3,4,5,4,5,3,4,3,5,3,4,5,4,3,4,5,4,3};
int [] scores1 = {3,4,3,5,3,4,4,3,5,3,3,4,3,4,3,4,3,4};
int [] scores2 = {4,4,3,5,3,4,4,3,5,3,3,4,3,4,3,4,3,4};
int [] scores3 = {3,4,3,5,3,4,4,3,5,3,3,4,3,4,3,4,3,5};
Tournament T = new Tournament(par);
T.enter("Norman", 2, scores1);
T.enter("Palmer", 4, scores2);
T.enter("Scott", 1, scores3);
ArrayList<String> winners = T.winners();
assertTrue(winners.get(0).equals("Norman"));
}
任何帮助将不胜感激。
【问题讨论】:
-
首先创建一个空列表,然后测试这个空列表是否为空,如果为 o,则返回 null。所以你总是返回null。你不应该,永远。阅读该方法的 javadoc。
-
您确定您的获胜分数与您认为的一样吗?