Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6975117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:19:38+00:00 2026-05-27T17:19:38+00:00

I want to make a Java game. At first the program asks for the

  • 0

I want to make a Java game. At first the program asks for the number of the players; after that, it asks for their names. I put their names in a HashMap with an ID and their score. At the end of the game I count the score and I want to put it in the HashMap (the specific score for the specific name). Does anyone know how to do this? This is my code:

Player:

public class Player {

public Player() {
}

public void setScore(int score) {
    this.score = score;
}

public void setName(String name) {
    this.name = name;
}

private String name;
private int score;

public Player(String name, int score) {
    this.name = name;
    this.score = score;
}
 public String getName() { 
     return name;
 }

@Override
public String toString() {
    return "Player{" + "name=" + name + "score=" + score + '}';
}

public int getScore() {
    return score;
}

main:

Scanner scanner = new Scanner(System.in);
HashMap<Integer,Player> name= new HashMap<Integer,Player>();

    System.out.printf("Give the number of the players ");
    int number_of_players = scanner.nextInt();


    for(int k=1;k<=number_of_players;k++)
     {


         System.out.printf("Give the name of player %d: ",k);
         name_of_players= scanner.nextLine();
         name.put(k, new Player(name_of_players,0));//k=id and 0=score

     }


   //This for finally returns the score and          
    for(int k=1;k<=number_of_players;k++)
  {  
      Player name1 = name.get(k);
     System.out.print("Name of player in this round:"+name1.getName());
    ..............
    .............
    int score=p.getScore();
    name.put(k,new Player(name1.getName(),scr));//I think here is the problem

    for(int n=1;n<=number_of_players;n++)//prints all the players with their score
      {

     System.out.print("The player"+name1.getName()+" has "+name1.getScore()+"points");

      }

Does anyone know how can I finally print for example:

"The player Nick has 10 points.
 The player Mary has 0 points."  

Update:

I did this in main(as Jigar Joshi suggest)

 name.put(k,new Player(name1.getName(),scr)); 
 Set<Map.Entry<Integer, Player>> set =   name.entrySet();

 for (Map.Entry<Integer, Player> me : set) 
 { 
 System.out.println("Score :"+me.getValue().getScore() +" Name:"+me.getValue().getName()); 

}

and it prints “Score :0 Name : a Score :4 Name : a” when i put two names of players “a” and “b”.I think the problem is here

 name.put(k,new Player(name1.getName(),scr));

How can I put the names in “names_of_players” of my previous for?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:19:39+00:00Added an answer on May 27, 2026 at 5:19 pm

    Need Key & Value in Iteration

    Use entrySet() to iterate through Map and need to access value and key:

    Map<String, Person> hm = new HashMap<String, Person>();
    
    hm.put("A", new Person("p1"));
    hm.put("B", new Person("p2"));
    hm.put("C", new Person("p3"));
    hm.put("D", new Person("p4"));
    hm.put("E", new Person("p5"));
    
    Set<Map.Entry<String, Person>> set = hm.entrySet();
    
    for (Map.Entry<String, Person> me : set) {
      System.out.println("Key :"+me.getKey() +" Name : "+ me.getValue().getName()+"Age :"+me.getValue().getAge());
    
    }
    

    Need Key in Iteration

    If you want just to iterate over keys of map you can use keySet()

    for(String key: map.keySet()) {
         Person value = map.get(key); 
    }
    

    Need Value in Iteration

    If you just want to iterate over values of map you can use values()

    for(Person person: map.values()) {
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know how I can make a Java program where an unknown
I'm writing a game in Java and this is the first one that I'm
I have a game server which is made in Java. I want to make
If I want to make my Java program to be able accept plugins, how
I want to make a program java desktop . I have 2 table, Table
I want to make Java JDialog appear like it is presented in the image
I want to make a sort in Java. In my object I have many
I want to make searching on my database with hibernate in Java, how can
I want to make an FTP connection (ideally using Coldfusion 8, but Java is
I want to see in action howto make blog on Eclipse + Java +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.