+ 3
[SOLVED]bowling game in java
may somebody tell me why every cases in result tab are true except the last one when I run my code. I wrote two different code one of them I commented below another one https://code.sololearn.com/cR9049D8e8d0/?ref=app
3 Antworten
- 1
with input "AAA 100\nBBB 200\nCCC 300\n"
after Bowling() you get:
al: [100, 200, 300]
players: {AAA=100, CCC=300, BBB=200}
ok
but after for() in main() you get:
al:[100, 200, 300, 300, 300, 300]
al is longer, because the input is "CCC 300"
and you add 300 at end of existed al 3x (!)
then in getWinner():
if (entry.getValue() == al.get(al.size() -1))
you compare every players.value with 300,
all tests are false because you compare Objects and not their values (!)
use this instead
if (entry.getValue() .equals ( al.get(al.size() -1)) ) {
is this answer to your question?
0
zemiak such a good explanation thanks you so much
- 1
I converted the hashmap into two arraylists one for keys and the other one for values. Using the I found out the maximum value among the values and used that to find its corresponding key.
https://code.sololearn.com/ca19A17A13a1/#