这个简单的游戏要求玩家人数和他们的名字,并计算他们的分数。我怎样才能得到最高分的玩家?我如何访问HashMap中的特定值?
主:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
HashMap<String,Integer> players= new HashMap<String,Integer>();
System.out.printf("Give the number of the players: ");
int numOfPlayers = scanner.nextInt();
for(int k=1;k<=numOfPlayers;k++)
{
System.out.printf("Give the name of player %d: ",k);
String nameOfPlayer= scanner.next();
players.put(nameOfPlayer,0);//score=0
}
//This for finally returns the score
for(String name:players.keySet())
{
System.out.println("Name of player in this round: "+name);
//::::::::::::::::::::::
//::::::::::::::::::::::
int score=players.get(name)+ p.getScore();;
//This will update the corresponding entry in HashMap
players.put(name,score);
System.out.println("The Player "+name+" has "+players.get(name)+" points ");
}
}
这是我尝试过自己:
Collection c=players.values();
System.out.println(Collections.max(c));
你已经有了一个如何遍历地图中所有条目的例子,并获得每个玩家的分数。你不知道如何做数字比较?你有什么尝试过自己? – 2012-01-06 14:59:09
p.getScore()是做什么的? – 2012-01-06 15:03:41