为了账号安全,请及时绑定邮箱和手机立即绑定

如何在最佳成绩历史记录活动中添加取得最佳成绩的玩家的姓名?

如何在最佳成绩历史记录活动中添加取得最佳成绩的玩家的姓名?

ibeautiful 2023-09-20 16:03:03
我有一个带有测验和活动的应用程序,我希望获得前 5 名得分的玩家。我有 2 个 ArrayList,其中一个存储分数,另一个存储玩家的姓名。我设法用分数列出了清单,但我不知道如何在同行分数旁边列出正确的名字。我所拥有的图像:https ://i.stack.imgur.com/7PxmR.png以及我正在寻找的图像:https://i.stack.imgur.com/HNEsU.png我尝试过使用 Map,但我不知道不能存储 2 个相同的键,所以这是不可能的,但可能有一种方法可以拥有某种带有索引的数组列表,但可以有 2 个不同的信息,我不知道知道。我的 2 个 ArrayList :public static ArrayList<Integer> scoresList = new ArrayList<>(); public static ArrayList<String> namesList = new ArrayList<>();我获得 5 个最佳成绩的方法:public class HistoryActivity extends AppCompatActivity {    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_history);        TextView first = (TextView) findViewById(R.id.history_activity_first);        TextView second = (TextView) findViewById(R.id.history_activity_second);        TextView third = (TextView) findViewById(R.id.history_activity_third);        TextView fourth = (TextView) findViewById(R.id.history_activity_fourth);        TextView fifth = (TextView) findViewById(R.id.history_activity_fifth);        Collections.sort(scoresList, Collections.reverseOrder());        first.setText("Best score is : " + scoresList.get(0));        if (scoresList.size() >= 2) {            second.setText("2nd best score is : " + scoresList.get(1));        }        if (scoresList.size() >= 3) {            third.setText("3rd best score is : " + scoresList.get(2));        }        if (scoresList.size() >= 4) {            fourth.setText("4th best score is : " + scoresList.get(3));        }        if (scoresList.size() >= 5) {            fifth.setText("5th best score is : " + scoresList.get(4));        }    }}
查看完整描述

1 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

创建一个类来表示分数/名称条目:


public class ScoreEntry implements Comparable<ScoreEntry> {

    public final String name;

    public final int score;


    public ScoreEntry (String name, int score){

        this.name = name;

        this.score = score;

    }


    public int compareTo (ScoreEntry other){

        return Integer.signum(score - other.score);

    }

}

然后你可以将它们放入 ArrayList 中。通过像这样实现 Comparable,您可以允许列表按分数排序。


您可能还想在此类中包含一个日期,以便较早日期取得的分数排名高于具有相同分数的其他条目。System.nanoTime()当得分时,您可以使用long 来获取时间。


public class ScoreEntry implements Comparable<ScoreEntry> {

    public final String name;

    public final int score;

    public final long time;


    public ScoreEntry (String name, int score, long time){

        this.name = name;

        this.score = score;

        this.time = time;

    }


    public int compareTo (ScoreEntry other){

        if (score == other.score)

            return Long.signum(other.time - time);

        return Integer.signum(score - other.score);

    }

}

编辑:如果您想通过其他方式排序,您需要一个自定义比较器。我根据这个答案改编了这个,它考虑了大写。


Comparator<ScoreEntry> nameComparator = new Comparator<>(){

    public int compare(ScoreEntry first, ScoreEntry second) {

        int res = String.CASE_INSENSITIVE_ORDER.compare(first.name, second.name);

        if (res == 0)

            res = first.name.compareTo(second.name);

        return res;

    }

}

然后你将其传递给排序方法:


Collections.sort(scoresList, nameComparator);


查看完整回答
反对 回复 2023-09-20
  • 1 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信