用C计算BMI

-4 c

#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;
}
Run Code Online (Sandbox Code Playgroud)

//我的BMI结果使计算保持为0.

我做错了什么.

Ita*_*ela 5

尝试这样做:

#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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 在没有**强**原因的情况下,更喜欢`double`到`float` (2认同)