亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在最佳成績歷史記錄活動中添加取得最佳成績的玩家的姓名?

如何在最佳成績歷史記錄活動中添加取得最佳成績的玩家的姓名?

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 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號