C 如果只给出if答案,则为else

C 如果只给出if答案,则为else,c,string,if-statement,conditional-statements,adventure,C,String,If Statement,Conditional Statements,Adventure,我正在为我的计算机科学课做一个项目。我们要做一个文字冒险游戏。除了最后的循环外,我几乎让所有的东西都工作了。if语句工作正常,但else-if语句只给出if语句的答案,而不是我为其编写的单独语句。我尝试过很多不同的写作方法,但我真的看不出问题所在。我将包括整个代码,以防在循环之外的某个地方出现问题 #include <stdio.h> int main() { char name[1024]; char kname[1024]; char response[1

我正在为我的计算机科学课做一个项目。我们要做一个文字冒险游戏。除了最后的循环外,我几乎让所有的东西都工作了。if语句工作正常,但else-if语句只给出if语句的答案,而不是我为其编写的单独语句。我尝试过很多不同的写作方法,但我真的看不出问题所在。我将包括整个代码,以防在循环之外的某个地方出现问题

#include <stdio.h>
int main() {
    char name[1024];
    char kname[1024];
    char response[1024];
    printf("Wanna play a game? First, I need your name.\n");
    scanf("%s", name);
    printf("Your name is %s, huh? I like it.\n", name);
    printf("Let's get started. You're in the woods. You come across a cat, now you need to name them.\n");
    scanf("%s", kname);
    printf("So, you decided to name them %s? Not what I would've picked, but it's not my pet.\n", kname);
    printf("You come across a plague doctor. Is that why you came into the woods, to get away from the sickness? Not exactly brave. Anyway, I digress. The plague doctor promises the impossible, tells you that he can and will make you immune with an elixir. Do you take it?\n");
    scanf("%s", response);
    if (response, "yes") {
        printf("You took it, huh? Not what I would've picked, but hey, you seem fine. Uh oh, you don't look so good. I...don't think that was a good idea. Well, there's nothing I can do for you now. Maybe restart the game and see if you can make a better decision. Until next time, old friend.");
    } else if (response, "no") {
        printf("Good choice. Come on, I think there's a town up ahead. I'd list off what the town's name was and all that can be found there, but it's currently 11:32 pm on a Saturday and our creator is halfway convinced that this entire program is a fever dream.");
    }
    return 0;
}
#包括
int main(){
字符名[1024];
char-kname[1024];
字符响应[1024];
printf(“想玩游戏吗?首先,我需要你的名字。\n”);
scanf(“%s”,名称);
printf(“你的名字是%s,嗯?我喜欢它。\n”,名字);
printf(“让我们开始吧。你在树林里。你遇到一只猫,现在你需要给它们命名。\n”);
scanf(“%s”,kname);
printf(“那么,你决定给它们命名为%s?不是我会选择的,但它不是我的宠物。\n”,kname);
printf(“你遇到了一位瘟疫医生。这就是你为什么来到树林里,以摆脱疾病的原因吗?不太勇敢。无论如何,我离题了。瘟疫医生承诺不可能,告诉你他可以而且会让你用长生不老药免疫。你服用了吗?\n”);
scanf(“%s”,响应);
如果(回答“是”){
printf(“你拿走了,嗯?不是我会选的,但是,嘿,你看起来很好。哦,你看起来不太好。我…不认为这是个好主意。好吧,现在我帮不了你。也许重新开始游戏,看看你能不能做出更好的决定。下次再做,老朋友。”);
}否则,如果(回答“否”){
printf(“不错的选择。来吧,我想前面会有一个城镇。我会列出这个城镇的名字和所有可以在那里找到的东西,但现在是周六晚上11:32,我们的创作者有点相信整个计划是一个狂热的梦想。”);
}
返回0;
}
如果(响应,“是”)
如果(响应,“否”)
没有针对您想用它做的事情进行适当的条件测试-比较两个字符串,并通过结果控制流,无论是否存在等效项

(回答“是”)
计算为
1
,其中
if
语句的条件将始终为真。因此,
else if
-语句或此后的语句(如果有多个)将永远不会执行

使用
strcmp()
比较两个字符串

if(response,“yes”)
更改为
if(!strcmp(response,“yes”))
else if(response,“no”)
更改为
if else(!strcmp(response,“no”))
,一切都应该正常


不要忘记通过
#include
包含
string.h的标题以使用
strcmp()

这里的问题是
if(response,“yes”)
不是检查响应是否等于yes的正确方法。请尝试
if(!strcmp(response,“res”))
。if语句中的逗号可能与您认为的不同。请改用类似strncmp()的函数。看到这个了吗