- 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
24 Antworten
+ 27
Hello I think I can help you.Check my code https://code.sololearn.com/cZUm3Sc5hFrJ/?ref=app
+ 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();
}
}
+ 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();
}
}
+ 3
If you want pre-made code then why not download the game itself ?
https://code.sololearn.com/W26q4WtwSP8W/?ref=app
+ 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 ?
+ 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 !
?
+ 1
can you help me with the output
0
Michał Skiba can you explain the meaning of the codes? Thanks
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);
}
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();
}
}
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();
}
}
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());
}
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);
}
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();
}
}
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();
}
}
0
Nice, it has been helpful
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);
}
}
}
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);
}
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();
}
}
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/