Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
有人可以向我解释吗?我做错什么了吗?
当我运行程序时,它没有显示正确的答案。

例如:当我输入体重= 50公斤,身高= 130厘米时,答案应该是


  “您的BMI是29.58。您超重2。您将有机会引起高血压,糖尿病需要控制饮食。还有健身。”


但显示的答案是


  “您的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();
}

最佳答案

在您的代码中,您尝试过的

 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对应,这当然不是您想要的。

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

10-16 03:41