创建分数最低的名称的arraylist

时间:2016-11-06 11:39:31

标签: java arraylist methods

我有一个锦标赛课程,我试图通过获得最低分并从中获得一个arraylist来获得胜利者。我想也许我可以将我的winnerScore方法用于我的获胜者方法?

这是我的尝试: (但我最终得到一个错误,因为它们的类型不同)

/**
 * 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;
}

我有这个方法,是否有某种方法可以将其用于获胜者方法?

/*
 * 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"));
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我不能单独留下这个。改进:

@Test(timeout=3000)
public void testWinners() {
    Tournament t = new Tournament();
    // int [] par = {3,4,5,4,5,3,4,3,5,3,4,5,4,3,4,5,4,3}; // par does not matter
    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};
    t.enter("Norman", 2, scores1);
    t.enter("Palmer", 4, scores2);
    t.enter("Scott", 1, scores3);
    assertTrue(winners.get(0).equals("Palmer"));
}

上课:

public class Tournament {

  List<Player> players = new ArrayList<>();

  private void enter(String name, int par, int[] scores) {
    players.add(new Player(name, par, scores));
  }

  public List<Player> getWinners() {
    List<Player> ps = new ArrayList<Player>(players);
    Collections.sort(ps);
    return ps;
  }

  private class Player implements Comparable<Player> {

    public String name;
    public int totalScore;

    public Player(String name, int par, int[] scores) {
        this.name = name;
        for (int score : scores) {
            totalScore += score;
        }
        //System.out.println("   " + name + " " + totalScore + ", par " + par + ", total " + (totalScore - par));
        totalScore -= par;
    }

    @Override
    public int compareTo(Player o) {
        return Integer.compare(totalScore, o.totalScore);
    }

  }

}