如何迭代Hashmap中的元素?

时间:2011-12-27 11:33:10

标签: java

我想制作一款Java游戏。首先,程序会询问玩家的数量;之后,它要求他们的名字。我把他们的名字放在HashMap的ID和他们的分数。在游戏结束时,我计算得分,我想把它放在HashMap(特定名称的具体分数)中。有谁知道如何做到这一点?这是我的代码:

播放器:

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;
}

主要

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");

      }

有谁知道我怎么能最终打印例如:

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

更新

我主要是这样做的(正如Jigar Joshi建议的那样)

 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()); 

}

并打印出“得分:0名称:得分:4名称:a”当我输入两个名字的球员“a”和“b”时。我认为问题出现在这里

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

如何将名称放在我之前for的“names_of_players”中?

4 个答案:

答案 0 :(得分:49)

需要钥匙和钥匙迭代中的值

使用entrySet()遍历Map并需要访问值和密钥:

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());

}

需要迭代密钥

如果您只想迭代keys地图,可以使用keySet()

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

迭代需要值

如果您只想迭代values地图,可以使用values()

for(Person person: map.values()) {

}

答案 1 :(得分:3)

你不应该将得分映射到玩家。你应该将玩家(或他的名字)映射到得分:

Map<Player, Integer> player2score = new HashMap<Player, Integer>();

然后添加玩家到地图: int score = .... Player player = new Player(); player.setName( “约翰”); //等 player2score.put(球员,得分);

在这种情况下,任务很简单:

int score = player2score.get(player);

答案 2 :(得分:3)

由于所有玩家都已编号,我只会使用ArrayList<Player>()

这样的东西
List<Player> players = new ArrayList<Player>();

System.out.printf("Give the number of the players ");
int number_of_players = scanner.nextInt();
scanner.nextLine(); // discard the rest of the line.

for(int k = 0;k < number_of_players; k++){
     System.out.printf("Give the name of player %d: ", k + 1);
     String name_of_player = scanner.nextLine();
     players.add(new Player(name_of_player,0)); //k=id and 0=score
}

for(Player player: players) {  
    System.out.println("Name of player in this round:" + player.getName());

答案 3 :(得分:1)

HashMap<Integer,Player> hash = new HashMap<Integer,Player>();
Set keys = hash.keySet();   
Iterator itr = keys.iterator();

while(itr.hasNext()){
    Integer key = itr.next();
    Player objPlayer = (Player) hash.get(key);
    System.out.println("The player "+objPlayer.getName()+" has "+objPlayer.getScore()+" points");
}

您可以使用此代码以您的格式打印所有分数。