gpt4 book ai didi

关系运算符的链接给出了错误的输出

转载 作者:行者123 更新时间:2023-11-30 21:17:48 25 4
gpt4 key购买 nike

谁能给我解释一下吗?我做错什么了吗?当我运行该程序时,它没有显示正确的答案。

例如:当我输入体重 = 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))

检查bmi[18.5, 23)其他情况依此类推。

<小时/>

编辑:

只是为了详细说明问题,像这样的表达式

18.5<=bmi<23

是完全有效的 C 语法。但本质上是一样的

((18.5<=bmi) < 23 )

由于 operator associativity .

所以,首先是 (18.5<=bmi)被评估并且结果(0或1)得到与23进行comapred ,这当然不是您想要的。

关于关系运算符的链接给出了错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35059133/

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