gpt4 book ai didi

用 C 计算 BMI

转载 作者:行者123 更新时间:2023-11-30 21:14:24 27 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int userAgeYears = 0;
int userAgeDays = 0;
int userWeight = 0;
int userHeight = 0;
int BMI;
printf("Enter your age in years: \n");
scanf("%d", &userAgeYears);
userAgeDays = userAgeYears * 365;
printf("Enter your weight in pounds: \n");
scanf("%d", &userWeight);
printf("Enter your height in inches: \n");
scanf("%d", &userHeight);
BMI = ((userWeight/(userHeight * userHeight)) * 703);
printf("You are %d days old.\n", userAgeDays);
printf("Your BMI is: %d\n", BMI);
return 0;
}

//我的 BMI 结果一直计算为 0。

我做错了什么吗?

最佳答案

尝试这样做:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
int userAgeYears = 0;
long userAgeDays = 0;
int userWeight = 0;
int userHeight = 0;
double BMI;

printf( "Enter your age in years: \n" );
scanf( "%d", &userAgeYears );
userAgeDays = userAgeYears * 365;
printf( "Enter your weight in pounds: \n" );
scanf( "%d", &userWeight );
printf( "Enter your height in inches: \n" );
scanf( "%d", &userHeight );

BMI = ( userWeight / (double)(userHeight * userHeight) ) * 703;

printf( "You are %ld days old.\n", userAgeDays );
printf( "Your BMI is: %02f\n", BMI );

return 0;
}

发生这种情况是因为您将所有变量定义为整数,并将 userWeight/(UserHeight*UserHeight) 的答案四舍五入为 0

关于用 C 计算 BMI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32680276/

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