【发布时间】:2016-04-11 17:34:08
【问题描述】:
我用字符串比较制作了这个笑话程序,但是当我输入“yes”时,它也会输入我对else 语句的内容。当我输入“no”时它不会这样做,如果我输入“yes”或“no”以外的其他内容,它会做出可预测的响应。
#include<stdio.h>
#include<string.h>
int main ()
{
char ansr[50];
printf("Are you a cop?");
scanf("%s", &ansr);
if (strcmp(ansr, "yes") == 0)
{
printf("Then get outta here buddy!");
}
if (strcmp(ansr, "no") == 0)
{
printf("Then you can learn the secret handshake!");
}
else
printf("\nDude! Yes or no question! Are you a cop?!\n");
}
【问题讨论】:
-
用
else if (strcmp(ansr, "no") == 0)替换if (strcmp(ansr, "no") == 0) -
我错过了您将“ansr”设置为什么的地方吗?
-
缩进/格式化。接下来...
-
您必须阅读用户输入,才能阅读用户输入。