我正在制作一個基于知識的游戲,用戶參與其中,如果他們獲勝并在標準范圍內獲得排名,他們將獲得寶石、硬幣等。
示例 - 一個游戲,其中有500 名參與者,并且預定義的獎品分配是 Like...
Rank 1 gets 50000 coins
Rank 2 gets 40000 coins
Rank 3 to 50 gets 20000 coins
Rank 51 to 200 gets 5000 coins
Rank 201 to 500 gets 1000coins
如果用戶玩游戲并假設他的排名為 81 。那么我如何檢查分配并獎勵他獎品,即 5000 個硬幣。
我可以使用 Hashmap 來創建某種鍵值對嗎..如下所示..
HashMap<Integer, Integer> distribution = new HashMap<>();
distribution.add(1,50000);
.
.
.
distribution.add(500,1000);
任何建議都會促進我的作業。
uj5u.com熱心網友回復:
如果你想使用 HashMaps,你可以做一些回圈。
HashMap<Integer, Integer> distribution = new HashMap<>();
//first and second place
distribution.add(1,50000);
distribution.add(2,40000);
//3rd through 50th place
for(int i = 3; i < 51; i )
distribution.add(i,20000);
//51 through 200th place
for(int i = 51; i < 201; i )
distribution.add(i, 5000);
//201 through 500th place
for (int i = 201; i < 501; i )
distribution.add(i, 1000);
和多田你完成了!但是,除非您希望它每次都運行,否則我會將分布設為靜態變數,這樣您只需運行一次,此后每次運行都會保持不變。
uj5u.com熱心網友回復:
我建議您創建一個 Range 包裝器類并查找該范圍內某個鍵的值;這可能會有所幫助:https : //stackoverflow.com/a/1314708/10052779
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/325192.html
