关系运算符的链接给出了错误的输出
Chaining of Relational operators is giving wrong output
谁能给我解释一下?我做错什么了吗?
当我 运行 程序时,它没有显示正确答案。
例如:当我输入体重 = 50 公斤和身高 = 130 厘米时,答案应该是
"Your BMI is 29.58. You are overweight2.You will have a chance to cause high blood pressure and diabetes need to control diet. And fitness."
但显示的答案是
"Your BMI is 29.58. You are normal......"
#include<stdio.h>
#include<conio.h>
int main() {
float weight, height, bmi;
printf("\nWelcome to program");
printf("\nPlease enter your weight(kg) :");
scanf("%f", &weight);
printf("\nPlease enter your height(cm) :");
scanf("%f", &height);
bmi=weight/((height/100)*(height/100));
if(bmi<18.50) {
printf("Your bmi is : %.2f",bmi);
printf("You are Underweight.You should eat quality food and a sufficient amount of energy and exercise proper.");
} else if(18.5<=bmi<23) {
printf("Your bmi is : %.2f \nYou are normal.You should eat quality food and exercise proper.",bmi);
} else if(23<=bmi<25) {
printf("Your bmi is : %.2f \nYou are overweight1 if you have diabetes or high cholesterol,You should lose weight body mass index less than 23. ",bmi);
} else if(25<=bmi<30) {
printf("Your bmi is : %.2f \nYou are overweight2.You will have a chance to cause high blood pressure and diabetes need to control diet. And fitness.",bmi);
} else if(bmi>=30) {
printf("Your bmi is : %.2f \nYou are Obesity.Your risk of diseases that accompany obesity.you run the risk of highly pathogenic. You have to control food And serious fitness.",bmi);
} else {
printf(" Please try again! ");
}
return 0;
getch();
}
在您的代码中,您已经尝试过
else if(18.5<=bmi<23)
不,这种关系运算符的链接在C中是不可能的。你应该写
else if((18.5<=bmi) && (bmi < 23))
检查 [18.5, 23)
中的 bmi
值,其他情况依此类推。
编辑:
只是为了详细说明问题,像
这样的表达
18.5<=bmi<23
是完全有效的 C 语法。不过本质上和
是一样的
((18.5<=bmi) < 23 )
因此,首先对 (18.5<=bmi)
求值,然后将结果(0 或 1)与 23
比较,这当然不是您想要的。
这个:
18.5<=bmi<23
与
的含义相同
(18.5 <= bmi) < 23
换句话说,bmi
的值永远不会与 23
进行比较。在C里一定不能用那种math-syntax,应该写成:
(18.5 <= bmi) && (bmi < 23)
所以它实际上是两次变量的比较,并且必须使用boolean-and(&&
)运算符来编写以使其显式。
是的,C 语言确实向您展示了您所尝试的正确答案。
C 中没有这样的语法用于比较(但是是的,这是一个有效的语法):
else if(18.5<=bmi<23)
应该是这样的:
else if((18.5<=bmi)&&(bmi < 23))
谁能给我解释一下?我做错什么了吗? 当我 运行 程序时,它没有显示正确答案。
例如:当我输入体重 = 50 公斤和身高 = 130 厘米时,答案应该是
"Your BMI is 29.58. You are overweight2.You will have a chance to cause high blood pressure and diabetes need to control diet. And fitness."
但显示的答案是
"Your BMI is 29.58. You are normal......"
#include<stdio.h>
#include<conio.h>
int main() {
float weight, height, bmi;
printf("\nWelcome to program");
printf("\nPlease enter your weight(kg) :");
scanf("%f", &weight);
printf("\nPlease enter your height(cm) :");
scanf("%f", &height);
bmi=weight/((height/100)*(height/100));
if(bmi<18.50) {
printf("Your bmi is : %.2f",bmi);
printf("You are Underweight.You should eat quality food and a sufficient amount of energy and exercise proper.");
} else if(18.5<=bmi<23) {
printf("Your bmi is : %.2f \nYou are normal.You should eat quality food and exercise proper.",bmi);
} else if(23<=bmi<25) {
printf("Your bmi is : %.2f \nYou are overweight1 if you have diabetes or high cholesterol,You should lose weight body mass index less than 23. ",bmi);
} else if(25<=bmi<30) {
printf("Your bmi is : %.2f \nYou are overweight2.You will have a chance to cause high blood pressure and diabetes need to control diet. And fitness.",bmi);
} else if(bmi>=30) {
printf("Your bmi is : %.2f \nYou are Obesity.Your risk of diseases that accompany obesity.you run the risk of highly pathogenic. You have to control food And serious fitness.",bmi);
} else {
printf(" Please try again! ");
}
return 0;
getch();
}
在您的代码中,您已经尝试过
else if(18.5<=bmi<23)
不,这种关系运算符的链接在C中是不可能的。你应该写
else if((18.5<=bmi) && (bmi < 23))
检查 [18.5, 23)
中的 bmi
值,其他情况依此类推。
编辑:
只是为了详细说明问题,像
这样的表达18.5<=bmi<23
是完全有效的 C 语法。不过本质上和
是一样的((18.5<=bmi) < 23 )
因此,首先对 (18.5<=bmi)
求值,然后将结果(0 或 1)与 23
比较,这当然不是您想要的。
这个:
18.5<=bmi<23
与
的含义相同(18.5 <= bmi) < 23
换句话说,bmi
的值永远不会与 23
进行比较。在C里一定不能用那种math-syntax,应该写成:
(18.5 <= bmi) && (bmi < 23)
所以它实际上是两次变量的比较,并且必须使用boolean-and(&&
)运算符来编写以使其显式。
是的,C 语言确实向您展示了您所尝试的正确答案。
C 中没有这样的语法用于比较(但是是的,这是一个有效的语法):
else if(18.5<=bmi<23)
应该是这样的:
else if((18.5<=bmi)&&(bmi < 23))