このシンプルなゲームは、プレイヤーの数とその名前を尋ね、得点を数えます。最高得点を得られるのはどのようなプレイヤーですか?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));
マップのすべてのエントリを繰り返し処理し、各プレーヤーのスコアを取得する方法の例が既にあります。あなたは数値比較を行う方法を知らないのですか?あなたは何を試しましたか? –
'p.getScore()'は何をしていますか? –