I am making a knowledge based game , in which user participate and if they win and get their rank within criteria they earn gems , coins etc.
Example- A game in which their are 500 participants , and predefined Prize Distribution is 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
If User plays the game and suppose he gets the rank 81 . So how can I check the distribution and award him the prize i.e, 5000 coins.
Can i use Hashmap to create some kind of key-value pair ..as shown below..
HashMap<Integer, Integer> distribution = new HashMap<>();
distribution.add(1,50000);
.
.
.
distribution.add(500,1000);
Any suggestions will boost my work.
CodePudding user response:
Well you could do a few loops if you want to use 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);
and tada you are done! However, unless you want it to run every single time, I would make distribution a static variable so you only have to run it once and it will stay for every run after that.
CodePudding user response:
I would recommend you to make a Range wrapper class and lookup the value for a key in the range; This might help: https://stackoverflow.com/a/1314708/10052779