Can anyone has done the bowling game q of java ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can anyone has done the bowling game q of java ?

It's the last q of the Java course

15th Jul 2021, 11:09 AM
Shivi Sharma
Shivi Sharma - avatar
5 Answers
+ 4
Shivi Sharma You have to get key of max value so just print key, don't add in keys. public void getWinner(){ int max = Collections.max(players.values()); for (Map.Entry<String, Integer> entry : players.entrySet()) { // Iterate through the map if (entry.getValue() == max) { System.out.println(entry.getKey()); // Print the key with max value break; } } }
15th Jul 2021, 12:56 PM
A͢J
A͢J - avatar
0
The description is in the code below import java.util.*; import java.util.Map.*; public class Bowling { HashMap<String, Integer> players; Bowling() { players = new HashMap<String, Integer>(); } public void addPlayer(String name, int p) { players.put(name, p); } //your code goes here public void getWinner() { //takes the max number from HashMap values int maxValueinMap = (Collections.max(players.values())); //Loops through the HashMap to find the number for (Entry<String, Integer> entry : players.entrySet()) { if (entry.getValue().equals(maxValueinMap)) { //prints out the key from the max value System.out.println(entry.getKey()); } } } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;i++) { String input = sc.nextLine(); String[] values = input.split(" "); String name = values[0]; int points = Integer.parseInt(values[1]); game.addPlayer(name, points); } game.getWinner(); } }
6th Jun 2022, 2:24 PM
Ronni Tosun
- 1
This is my code it's giving error which I'm unable to understand
15th Jul 2021, 11:13 AM
Shivi Sharma
Shivi Sharma - avatar
- 2
Shivi Sharma Almost everyone did but where is your attempts?
15th Jul 2021, 11:10 AM
A͢J
A͢J - avatar
- 2
import java.util.*; public class Bowling { HashMap<String, Integer> players; Bowling() { players = new HashMap<String, Integer>(); } public void addPlayer(String name, int p) { players.put(name, p); } //your code goes here public void getWinner (){ int max = Collections.max(players.values ); List <String> keys = new ArrayList <> (); for (Map.Entry <String , Integer > entry : players.entrySet()) { if(entry.getValue() == max) { keys.add(entry.getKey()); } } } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;i++) { String input = sc.nextLine(); String[] values = input.split(" "); String name = values[0]; int points = Integer.parseInt(values[1]); game.addPlayer(name, poin
15th Jul 2021, 11:12 AM
Shivi Sharma
Shivi Sharma - avatar