Reputation: 323
This simple program asks for the number of players,their names and (in the last for) it counts their score.But I dont know why it doen't go to the next player.Look at my code:
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())
{
int k=1;
do{
System.out.println("Name of player in this round: "+name);
System.out.printf("Give me your word: ");
String nameOfWord= scanner.next();
//::::::::::::::::::::::
//::::::::::::::::::::::
int score=players.get(name)+10;
//This will update the corresponding entry in HashMap
players.put(name,score);
System.out.println("The Player "+name+" has "+players.get(name)+" points ");
}while(k>0);
}
} }
This returns:
Give the number of the players: 2
Give the name of player 1: A
Give the name of player 2: B
Name of player in this round: A
Give me your word: ABC
The Player A has 10 points
Name of player in this round: A
Give me your word:......
........
but i want after it counts the score for 'A' to count the score for 'B'.How can i do this?What am i doing wrong?
Upvotes: 0
Views: 102
Reputation: 11369
You don't change the variable k
anywhere in your do ... while
loop (which is the only variable contained in the loop condition) - meaning that it will loop forever with the first player!
If I'm interpreting correctly what you want to do, you should completely remove the do {
and the } while (k>0)
lines - you don't even reference the value of k
anywhere inside the loop, except for in the condition; is it maybe a remainder of iterating over the players in another way?
Or, if it should have been the loop for the doing the game rounds (one round being the collection of one turn for each player), then it's in the wrong position - it should be around the for ...
loop iterating over the players, not inside it; you'd of course also have to come up with a proper condition for the game to be over - if it really depends on the k
variable you're currently using, then this variable would also have to change somwhere, or your back to a neverending loop!
But it's hard to interpret your code without more information; please update your question with more information on how a full round should look like in your game, and what the condition is that the game is over!
Upvotes: 0
Reputation: 424993
Get rid of the do while
- you don't need it. Also, iterate "properly" over the Map
.
Here's the re-worked code fragment:
// Note this more elegant iteration over a map
for (Map.Entry<String, Integer> entry : players.entrySet()) {
String name = entry.getKey();
int score = entry.getValue();
System.out.println("Name of player in this round: " + name);
System.out.printf("Give me your word: ");
String nameOfWord = scanner.next();
// Some code that uses "nameOfWord"
score += 10;
entry.setValue(score); // Note this more elegant use of the API
System.out.println("The Player " + name + " has " + score + " points");
}
I must admit I don't understand what you're trying to do, but at least this code does what your code intended.
Upvotes: 1
Reputation: 1422
Why you are using do-while.I see no use of it.And ofcourse that is causing the problem as you are never increasing k and it seems to be infinte loop to me
Upvotes: 0