繁体   English   中英

如何迭代Hashmap中的元素?

[英]How can I iterate over the elements in Hashmap?

我想制作一个Java游戏。 首先,程序会询问玩家的数量; 之后,它要求他们的名字。 我把他们的名字放在一个带有ID及其分数的HashMap 在比赛结束时,我计算得分,我想把它放在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”和“b”。我认为问题在这里

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

我怎样才能把我以前的“names_of_players”名字for

在迭代中需要关键和价值

使用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); 
}

在迭代中需要价值

如果您只想迭代map的values ,可以使用values()

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

}

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

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

然后添加玩家到map:int score = ....玩家玩家=新玩家(); player.setName( “约翰”); //等等player2score.put(玩家,得分);

在这种情况下,任务是微不足道的:

int score = player2score.get(player);

由于所有玩家都已编号,我只会使用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());
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");
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM