gpt4 book ai didi

c - 需要帮助用 C 编写 BMI 计算器

转载 作者:太空宇宙 更新时间:2023-11-04 02:07:04 27 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序将接受用户输入的体重和高度,然后返回 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);
}
}

最佳答案

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);  

也不要在 printf 中对 double 使用 %lf 说明符(在 scanf 中你必须使用)使用 %f

关于c - 需要帮助用 C 编写 BMI 计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19505537/

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