#include <stdio.h>
int main(){
int age;
char gender;
printf("Are you male or female (m/f)?");
scanf("%c",&gender);
printf("Enter your age:");
scanf("%d",&age);
printf("\n");
if(gender=="m"){
if(age>=0 && age<=55){
printf("You are a man in your best years!\n");
}
else if (age>=56 && age<=100){
printf("You are a wise man!\n");}
else{
printf("An error occurred in the program!");}
}
else if(gender=="f"){
if(age>=0 && age<=55){
printf("You are a beauty!\n");
}
else if (age>=56 && age<=100){
printf("You look young for your age!\n");}
else{
printf("An error occurred in the program!");}
}
else{
printf("An error occurred in the program!");}
return 0;
}
程序运行不正常。它只打印最后的 else 语句打印,即“程序中发生错误!”。我还尝试将所有条件放在同一个小括号中
喜欢:
if(gender=="f"&& age>=0 && age<=55))
但仍然没有输出预期的结果。
最佳答案
您正在将 char
与 string
文字 (gender == "m")
进行比较。双引号用于字符串。在 m (gender == 'm')
和 f (gender == 'f')
周围使用单引号。
关于c - 仅打印 "An error occurred in the program!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40498725/