计算体重指数。 体重指数将您的体重与高度进行比较,计算方法是将您的体重(公斤)除以高度(米)的平方。它可以让您了解您是否体重不足、健康体重、超重或肥胖。
体重指数类别:
- 减持 = <18.5
- 正常体重 = 18.5–24.9
- 超重 = 25–29.9
- 肥胖 = BMI 等于或大于 30
如果您体重不足、超重或肥胖,请根据您的高度和年龄确定理想体重。
使用 Devine 公式估算理想体重(公斤):
男性:IBW = 50 kg + 2.3 kg for each inch over 5 feet.
女性:IBW = 45.5 kg + 2.3 kg for each inch over 5 feet.
#include <stdio.h>
#include <stdlib.h>
double calculateBMI(double weight, double height);
int main(void)
{
printf("Calculate your BMI\n"); //Calculation of body mass index (BMI)
double w, h, ret;
double BMI = w / (h*h);
ret = calculateBMI(BMI);
printf("Enter your weight in kilograms:\n", w); //Input your weight in kilograms here
scanf("%lf", &w);
printf("Enter your height in metres:\n", h); //Input your height in metres here
scanf("%lf", &h);
printf("Your BMI is %lf\n", ret)
printf("BMI categories:\n");
if (BMI < 18.5)
{
printf("Your BMI is %lf and you are currently underweight.\n");
}
else if (BMI >= 18.5 && BMI <= 24.9)
{
printf("Your BMI is %lf and you are normal weight.\n");
}
else if (BMI >= 25 && BMI <= 29.9);
{
printf("Your BMI is %lf and you are currently overweight.\n");
}
else (BMI >= 30);
{
printf("Your BMI is %lf and you are obese.\n");
}
return 0;
}
double calculateBMI(double weight, double height)
{
double result;
result = weight / (height*height);
return result;
}
帮助我仍然不知道如何执行这些功能。请帮助我。
最佳答案
您应该使用方法中使用的参数来计算 BMI。 您的变量 BMI、高度和体重最初并未在函数中声明。相反,您应该将 BMI 声明为双倍,并使用高度和体重作为函数参数。
此外,您还需要从函数返回 BMI 值。您错误地返回了calculateBMI,它不是函数内的有效标识符。
工作代码是:-
double calculateBMI(double weight, double height)
{
double BMI = weight / (height*height);
return BMI;
}
此外,您还没有在 main() 中调用方法calculateBMI()。
...
printf("Enter your weight in kilograms:\n"); //Input your weight in kilograms here
scanf("%lf", &weight);
printf("Enter your height in metres:\n"); //Input your height in metres here
scanf("%lf", &height);
// add below line in your code to call the function.
BMI = calculateBMI(height,weight);
printf("BMI categories:\n");
...
我还建议你阅读更多有关 C 语言中函数的内容。你需要一些更基本的函数知识(也要努力练习)。
EDIT ---> Based on OP's comment, the final code would be :
#include <stdio.h>
#include <stdlib.h>
double calculateBMI(double weight, double height);
int main(void)
{
printf("Calculate your BMI\n"); //Calculation of body mass index (BMI)
double w, h, BMI;
printf("Enter your weight in kilograms:\n", w); //Input your weight in kilograms here
scanf("%lf", &w);
printf("Enter your height in metres:\n", h); //Input your height in metres here
scanf("%lf", &h);
BMI = calculateBMI(w,h);
printf("Your BMI is %lf\n", BMI)
printf("BMI categories:\n");
if (BMI < 18.5)
{
printf("Your BMI is %lf and you are currently underweight.\n");
}
else if (BMI >= 18.5 && BMI <= 24.9)
{
printf("Your BMI is %lf and you are normal weight.\n");
}
else if (BMI >= 25 && BMI <= 29.9);
{
printf("Your BMI is %lf and you are currently overweight.\n");
}
else (BMI >= 30);
{
printf("Your BMI is %lf and you are obese.\n");
}
return 0;
}
double calculateBMI(double weight, double height)
{
double result;
result = weight / (height*height);
return result;
}
关于c - 如何使用函数计算BMI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36256768/