C语言 c语言编程刽子手
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22877160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
programming hangman in c
提问by user3483718
Write an interactive program that plays a game of hangman. Store the word (single word, not multiple words) in an array of characters called word. Create a parallel array called guessed in which there are *'s in place of the letters. If the player guesses correctly, put the letter into the guessed array. Give the player 5 wrong guesses to figure out the word. Assume that the word will be no more than 20 characters.
编写一个玩刽子手游戏的交互式程序。将单词(单个单词,而不是多个单词)存储在名为 word 的字符数组中。创建一个名为 guessed 的并行数组,其中用 * 代替字母。如果玩家猜对了,则将字母放入猜中的数组中。给玩家 5 次错误的猜测以找出单词。假设单词不超过 20 个字符。
so my program is compiling and all but the if statement is coming out wrong, it keeps showing me both statements the keep going and the try again.
所以我的程序正在编译,除了 if 语句之外的所有语句都出错了,它不断向我显示继续运行和再试一次的两个语句。
#include <stdio.h>
#include <conio.h>
//function prototypes
void game(char [], char []);
int main()
{
char word[20] = {'d', 'u','c','k'};
char guessed[20];
game(word,guessed);
getch();
return 0;
}
void game(char answer[], char guess[])
{
int x = 0;
char letter;
while (x < 6)
{
scanf("%c",&letter);
if (letter == answer[x])
{
guess[x]= letter;
printf("keep going\n");
}
else
{
printf("Try again\n");
}
++x;
}
}
回答by CHOCKO
Make a change in your code..
更改您的代码..
scanf(" %c",&letter);
// ↑
// Space before `%c`
Because when you read a value with scanf()'\n'left unread which you leave behind by pressing Enter key. To neglect '\n'you have to read a character with " %c"(Space before %c)
因为当您读取一个带有scanf()'\n'left unread的值时,您可以按Enter key. 忽略'\n'你必须用“%c”读取一个字符(%c之前的空格)

