#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;
}
程序无法正常运行。它只打印最后的其他语句打印,即&#34;程序中出错!&#34;。我还尝试将所有条件放在相同的小括号中
喜欢:
if(gender=="f"&& age>=0 && age<=55))
但仍未输出预期结果。
答案 0 :(得分:3)
您正在将char
与string
文字(gender == "m")
进行比较。双引号用于字符串。在m (gender == 'm')
和f (gender == 'f')
周围使用单引号。