【问题标题】:Creating an arraylist of names with lowest scores创建分数最低的名称数组列表
【发布时间】: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。
  • 您确定您的获胜分数与您认为的一样吗?

标签: java arraylist methods


【解决方案1】:

我不能让这个人一个人呆着。改进:

@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);
    }

  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2015-08-15
    • 2021-07-09
    • 2012-04-10
    相关资源
    最近更新 更多