- 9

Bowling game project

Hi, can somebody help me with bowling game project (java)... I tried it, but it doesnt work... I try it 5 days.. Please help

28th Nov 2020, 8:14 AM
MartinGames
MartinGames - avatar
24 ответов
+ 27
Hello I think I can help you.Check my code https://code.sololearn.com/cZUm3Sc5hFrJ/?ref=app
29th Nov 2020, 8:16 PM
mg thu kha
mg thu kha - avatar
+ 20
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() { System.out.println(players.entrySet() .stream().max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).get().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(); } }
5th Feb 2021, 12:32 PM
Md Momin Uddin Hridoy
Md Momin Uddin Hridoy - avatar
+ 4
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(){ String winner=""; String[] nameArr = new String[players.size()]; nameArr = players.keySet().toArray(nameArr); int max=0; for(String em:nameArr){ if(players.get(em)>max){ winner=em; max=players.get(em); } } System.out.println(winner); } } 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(); } }
1st Oct 2021, 9:55 AM
Hasnae BOUHMADY
Hasnae BOUHMADY - avatar
+ 3
If you want pre-made code then why not download the game itself ? https://code.sololearn.com/W26q4WtwSP8W/?ref=app
28th Nov 2020, 8:19 AM
Arsenic
Arsenic - avatar
+ 2
Do you except us to break into your computer to find what you have attempted till now or read your mind for that ?🤔 How do you want the community's help you ?
28th Nov 2020, 8:17 AM
Arsenic
Arsenic - avatar
+ 1
Hello ! I tried many times this bowling game program this program doesn't run test case 1 is solve but in test case 2 the output is A and expected output is B Can anyone help me pls ! ?
27th Jul 2021, 3:21 PM
Shivangi pandey
+ 1
can you help me with the output
4th Apr 2022, 2:55 PM
ABDULLAHI IBRAHIM
0
Michał Skiba can you explain the meaning of the codes? Thanks
4th May 2021, 4:51 AM
Kurniadi Kusuma
Kurniadi Kusuma - avatar
0
You can try something like this. Casting is needed because getKey() and getValue() returns a type Object. public void getWinner() { int max = 0; String maxPlayer = null; Iterator it = players.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); if ((int) pair.getValue() > max) { max = (int) pair.getValue(); maxPlayer = (String) pair.getKey(); } } System.out.println(maxPlayer); }
6th May 2021, 9:07 AM
Ng Tze Henn
Ng Tze Henn - avatar
0
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() { System.out.println(players.entrySet() .stream().max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).get().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(); } }
28th Aug 2021, 11:54 AM
Nishant Wagharalkar
Nishant Wagharalkar - avatar
0
You can also use the For-Each property as follows: 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(){ Integer maximum = 0; String maxPlayer = ""; for (Map.Entry<String,Integer> entry : players.entrySet()){ if((Integer)entry.getValue() > maximum){ maximum = (Integer)entry.getValue(); maxPlayer = (String)entry.getKey(); } } System.out.println(maxPlayer); } } 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(); } }
9th Sep 2021, 7:14 PM
SHREYASI GHOSH
SHREYASI GHOSH - avatar
0
//your code goes here public void getWinner() { System.out.println(players.entrySet() .stream().max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).get().getKey()); }
14th Sep 2021, 7:56 AM
APURVA NIGAM
APURVA NIGAM - avatar
0
//your code goes here public void getWinner(){ int maxVal = 0; String maxKey = ""; // https://stackoverflow.com/questions/46898/how-do-i-efficiently-iterate-over-each-entry-in-a-java-map for (Map.Entry<String, Integer> map: players.entrySet()){ if (map.getValue()>maxVal){ maxVal = map.getValue(); maxKey = map.getKey(); } } System.out.println(maxKey); }
11th Nov 2021, 11:24 AM
Milan Navodya
Milan Navodya - avatar
0
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() { System.out.println(players.entrySet() .stream().max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).get().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(); } }
12th Dec 2021, 2:09 AM
MN FATHIMA RIHAM - s92064451 -321424451
MN FATHIMA RIHAM - s92064451 -321424451 - avatar
0
this is my solution to the problem import java.util.*; import java.util.HashMap; import java.util.Map.Entry; public class Bowling { HashMap<String, Integer> players; Bowling() { players = new HashMap<String, Integer>(); } public void addPlayer(String name, int p) { players.put(name, p); } public void getWinner() { //arrayListto stor points ArrayList<Integer> point = new ArrayList<>(); //iterate over the players Hashmap and store values of it in the points arrayist Iterator<Integer> it =this.players.values().iterator(); while(it.hasNext()) { point.add(it.next()); } //sort the array List Collections.sort(point); //iterate over the palyers and get the Key with the maximum values for (Entry<String,Integer> entry : players.entrySet()) { if (entry.getValue() == point.get(point.size()-1)) { System.out.println(entry.getKey()); break; } } } } 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(); } }
20th Feb 2022, 10:48 AM
Abdelilah Boudribila
Abdelilah Boudribila - avatar
0
Nice, it has been helpful
2nd Apr 2022, 11:29 AM
Christian Afeku
Christian Afeku - avatar
0
import Collections class //your code goes here public void getWinner(){ int maximum = Collections.max(players.values()) ; String maxPlayer =" "; for (Map.Entry<String,Integer> entry : players.entrySet()){ if((Integer)entry.getValue() == maximum){ maximum = (Integer)entry.getValue(); maxPlayer = (String)entry.getKey(); System.out.println(maxPlayer); } } }
20th Apr 2022, 11:47 AM
Sanath Kumar
Sanath Kumar - avatar
0
//your code goes here public void getWinner(){ //variables I need for my code int score=0; String winner=""; //Creating an array of the playernames String[] nameArr= new String[players.size()]; nameArr=players.keySet().toArray(nameArr); //assigning the winner to the winner variable for(String a: nameArr){ if(players.get(a)> score ){ score = players.get(a); winner = a; } } //output the winner System.out.println(winner); }
24th Apr 2022, 10:05 PM
Marloes van den Adel
0
i have tried it out according to me this is the simplest method try it if you want 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 cbs=0; String cw =""; for(String pn : players.keySet()) { if(players.get(pn)>=cbs) { cw=pn; cbs= players.get(pn); } } System.out.println(cw); } } 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(); } }
27th Sep 2022, 7:55 AM
Vaishnavi Singh
Vaishnavi Singh - avatar
0
my ans public void getWinner(){ int maxValueInMap = (Collections.max(players.values())); for (Entry<String,Integer> entry : players.entrySet()){ if(entry.getValue()== maxValueInMap){ System.out.println(entry.getKey()); } } } //it works well //reference:https://www.geeksforgeeks.org/how-to-find-the-entry-with-largest-value-in-a-java-map/
21st Aug 2023, 1:13 PM
Ting-Xun Chen
Ting-Xun Chen - avatar