我正在做一个学校项目的绞刑者游戏,但我面临着一个问题:
do{
system("cls");
// Header of the game
printf("\n HANGMAN GAME\n\n\n");
// Present letters found
for (i=0; word[i]!='\0'; i++)
printf (" %c ", word_2[i]);
printf("\n");
// Present positions to the letters
for (i=0; word[i]!='\0'; i++)
printf("___ ");
printf("\n");
// ****PLAYER'S ANSWERS*****
// Read player's answers
printf("\n\n Whats your guess + <enter>: ");
scanf("%c", &letter);
scanf("%c", &c);
// Verify if the letter is in the word
found=0;
for(i=0; word[i]!='\0'; i++)
if (word[i] == letter){
word_2[i] = letter;
corrects++;
max_attemps--;
found = 1;
printf("\nWell done, %s. You have now %d attempts\n\n", name, max_attemps);
system("pause");
}
if(found == 0){
max_attemps--;
printf("\nOh no, %s. You have now %d attempts\n\n", name, max_attemps);
system("pause");
}
if (max_attemps <= 0 || corrects == lenght) {
end = 1;
}
} while (end == 0);当我得到一个在单词中有两个或更多位置的正确字母时,它需要我进行两次或更多次尝试,因为system("pause"),而实际上我只能尝试一次。但是如果我不把system("pause")放进去,在我看到消息之前,板子就会被清除。有人知道我能做些什么来解决这个问题吗?我会非常感激的。
发布于 2019-12-08 19:07:32
只有在扫描完整个单词之后,才能检查found标志:
// Verify if the letter is in the word
found=0;
for(i=0; word[i]!='\0'; i++)
if (word[i] == letter){
word_2[i] = letter;
corrects++;
max_attemps--;
found = 1;
// comment out
// printf("\nWell done, %s. You have now %d attempts\n\n", name, max_attemps);
// system("pause");
}
if(found == 1)
{
printf("\nWell done, %s. You have now %d attempts\n\n", name, max_attemps);
system("pause");
}
else{
max_attemps--;
printf("\nOh no, %s. You have now %d attempts\n\n", name, max_attemps);
system("pause");
}
if (max_attemps <= 0 || corrects == lenght) {
end = 1;
}https://stackoverflow.com/questions/59234735
复制相似问题