gpt4 book ai didi

c - 当我运行程序时,最后一个 "if"语句不起作用

转载 作者:行者123 更新时间:2023-12-04 01:28:37 25 4
gpt4 key购买 nike

每次我运行程序时,最后一个“if”语句都不起作用,这意味着如果我输入“no”,循环就不会中断。有人可以帮我吗?

#include <stdio.h>

int main() {
int age, i;
char ans;

for (i = 0; i < 3; i++) {
printf("\n enter your age:");
scanf("%d", &age);

if (age > 18) {
printf("your age is %d, you are allowed to enter", age);
} else if (age == 18) {
printf("I don't know what to do with you");
} else {
printf("your age is %d, you are not allowed to go in", age);
}

printf("\n continue?");
scanf(" %c", &ans);

if (ans == 'no') { // <-- here
break;
} else {
continue;
}
}

return 0;
}

最佳答案

使用 if (ans == 'n')。如果要使用单词"no",则必须将变量ans 的类型更改为char 数组并使用strcmp() 方法比较字符串。

关于c - 当我运行程序时,最后一个 "if"语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61406088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com