【发布时间】:2020-11-11 19:31:37
【问题描述】:
我是编程新手,我想通过下面的一个简单的 C 程序来做这个猜谜游戏。当我输入单词“apple”中的字母时,除了字母“a”之外,每个字母 (p,l,e) 都会执行错误的猜测重试语句。我似乎无法理解我在这里做错了什么。任何见解都受到高度赞赏。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define size 10
int main() {
// initialize variables
char word[size] = "apple";
char guess;
char arrayofdashes[size] = "_____";
printf("guess a letter \n");
// input loop
for (int i = 0; i < strlen(word); i++)
{
scanf(" %c", &guess);
for (int j = 0; j< strlen(word); j++ )
{
if (guess == word[j])
{
arrayofdashes[j] = guess;
printf("%s \n", arrayofdashes);
}
else
{
printf("wrong guess. Try again \n");
break;
}
}
}
}
【问题讨论】:
-
如果您的猜测与单词的第一个字母不匹配,那么您将跳出 for 循环。
标签: c if-statement statements