任何人都可以向我解释这个吗?我做错什么了吗? 当我运行程序时,它没有给我正确的答案。
例如:当我打字重量= 50公斤,身高= 130厘米时,答案应为
"您的BMI是29.58。你超重。你将有机会引起高血压,糖尿病需要控制饮食。和健身。"
但显示的答案是
"您的BMI是29.58。你很正常......"
#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();
}
答案 0 :(得分:2)
在您的代码中,您尝试了什么
else if(18.5<=bmi<23)
不,这种链接的关系运算符在C中是不可能的。你应该写
else if((18.5<=bmi) && (bmi < 23))
检查bmi
中的[18.5, 23)
值,等等其他情况。
编辑:
只是要详细说明问题,像
这样的表达式18.5<=bmi<23
是完全有效的C语法。但是,它与
基本相同((18.5<=bmi) < 23 )
所以,首先评估(18.5<=bmi)
,然后将结果(0或1)与23
进行合作,这当然不是你想要的。
答案 1 :(得分:1)
此:
18.5<=bmi<23
与
相同(18.5 <= bmi) < 23
换句话说,bmi
的值永远不会与23
进行比较。你不能在C中使用那种数学语法,它应该写成:
(18.5 <= bmi) && (bmi < 23)
所以它实际上是两个变量的比较,并且必须使用boolean-and(&&
)运算符来编写它。
答案 2 :(得分:0)
是的,C语言确实向您显示您尝试过的正确答案。
C中没有这样的语法用于比较(但是,这是一个有效的语法):
else if(18.5<=bmi<23)
它应该是这样的:
else if((18.5<=bmi)&&(bmi < 23))