1

我正在尝试编写一个程序,该程序将接受用户输入的体重和身高,然后返回一个 BMI 值并告诉用户他们的体重是否低于/超过或正常。代码编译时没有错误,但是无论我为体重和身高输入什么数字,结果始终是“您的 BMI 为 0 并且您的体重状态超重”。我的代码有问题还是我的数学不正确?

#include <stdio.h>

int main()
{
    double wt_lb, ht_in, bmi, ht_ft;

    printf("Please enter your weight in whole pounds: ");
    scanf("%lf", &wt_lb);
    printf("Please enter your height in whole inches: ");
    scanf("%lf", &ht_in);

    ht_ft = ht_in/12;
    bmi = (703*wt_lb)/(ht_ft*ht_ft);

    if (bmi < 18.5) {
            printf("You have a BMI of %.lf, and your weight status is underweight\n" &bmi);
    } else if (bmi >= 18.5 && bmi < 25) {
        printf("You have a BMI of %.lf, and your weight status is normal\n", &bmi);
    } else {
        printf("You have a BMI of %.lf, and your weight status is overweight\n", &bmi);
    }
}
4

2 回答 2

2

&从你printf的论点 中删除。

 printf("You have a BMI of %f, and your weight status is underweight\n" &bmi);  
                                                                        ^
                                                                        |
                                                                  Remove this &  

它应该是

  printf("You have a BMI of %f, and your weight status is underweight\n", bmi);  

也永远不要使用in的%lf说明符(你必须使用)而不是使用. doubleprintfscanf%f

于 2013-10-21T22:02:49.737 回答
1

在 printf 语句中不要使用 &bmi,使用简单的 bmi。它应该工作

于 2013-10-21T22:20:32.500 回答