이 간단한 프로그램은 플레이어의 수와 이름을 묻습니다. (마지막으로) 점수를 계산합니다.하지만 다음 플레이어에게가는 이유는 모르겠습니다. 내 코드를보세요.어떻게이 권리를 인쇄 할 수 있습니까?
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);
}
} }
내가 뭘 잘못
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:......
........
하지만 나는 내가이 작업을 수행 할 수있는 'B'.How에 대한 점수를 계산하는'A '에 대한 점수를 계산 한 후 원하는 :
이 반환?
나는 이미 답변을 얻은 것으로 생각한다. http://stackoverflow.com/questions/8644176/how-can-i-add-elements-in-hashmap/8644194#8644194 –
'k' 변수를 보라! 당신은 1로 설정 한 다음'do {...} while (k> 0);'<- 조건이 항상 참이면 누락 된 코드에서 k를 감소시키지 않는 한 두 번째 항목을 순환하지 않습니다. – fge
@ JigarJoshi하지 않고 일하는 동안 작동하지만 지금은 내가 그들을 포함시켜야합니다 ... – nick