我怎样才能在C中解决这个问题?

我有一个模拟BMI(身体质量指数)计算器的程序,该计算器接收来自10个用户的输入,询问每个用户的重量和高度,然后程序计算BMI并在格式化的输出中打印计算结果。

像这样的东西:

USER BMI STATUS 1 : 10 

所以我想到了这一点,我会首先接受用户的输入,然后尝试以上面的格式化方式显示输入的计算。 以下是我试图提出的问题,一直向用户询问他们的体重和身高,直到第10位用户。

 #include  #include  int main(void) { float weight_value = 0.0f; float user_height = 0.0f; float BMI = 0.0f; char BMI_Status[30]; int i; for ( i = 1; i <= 10; i++) { printf("Please enter your weight: "); scanf("%f", &weight_value); //Reads in the user's weight printf("Now enter your height: "); scanf("%f", &user_height); //Reads in the user's height BMI = weight_value / (user_height * user_height); } return 0; } 

BMI状态是一个字符串,显示计算的BMI值的不同状态,当BMI值在给定范围内时,它们是"Underweight", "Normal", "Overweight"

上面的代码片段仍然不完整,因为我仍然发现很难添加其余的代码。 以下是我要解决的问题。

  1. 在接收10个输入后,如何打印出BMI值?

    我试过有一个嵌套的for循环打印出来的值,但这是不合逻辑的,因为现在外部for循环的每次迭代都会有BMI的输出,所以我猜想需要一种方法来制作scanf()在for循环块之后保持BMI值。

  2. 如何显示状态字符串?

    我知道C中没有string数据类型,所以我必须声明一个包含状态字符串的char数组,但这些是不同状态的不同字符串,并且必须为每一行打印出来。 我想if else if else条件,但我不能让char数组工作。

我已经成功编写了一个代码,可以在每次迭代时打印出用户的BMI,但这不是它应该如何完成的。

我想到的另一件事是将BMI值保存在浮点数数组中,然后以上面格式化的方式显示数组中的值,但我仍然对我打算如何做这一点持怀疑态度。

这里显示了使用数组来存储值的完整代码。 你使用if else来显示一个人超重的想法是好的,所以我实现了它。

 #include  #include  int main(void) { float weight_value[10]; //two arrays of ten elements each float user_height[10]; float BMI = 0.0f; char BMI_Status[30]; int i; for ( i = 0; i < 10; i++) //the array starts from zero in C { printf("Please enter your weight: "); scanf("%f", &weight_value[i]); //store the values in the array printf("Now enter your height: "); scanf("%f", &user_height[i]); //store the values in the array } printf("USER BMI\t\tSTATUS\n"); for( i = 0; i < 10; i++) //pass the array once again and calculate the needed values { BMI = weight_value[i] / (user_height[i] * user_height[i]); printf("%d\t%f\t", i+1, BMI); if(BMI < 30) printf("Underweight\n"); else if(BMI < 50) printf("Normal\n"); else printf("Overweight\n"); } return 0; } 

编辑:跳到最后

您需要将它们保存到某个地方才能读取它们以显示它,您需要使用一个数组来存储所有10个输入,此时每次用户输入数据时它将重写变量中的数据,有最后一个用户的数据。

它需要看起来像这样:

 ... float weight_value[10]; float user_height[10]; float BMI =[10]; char BMI_Status[30][3]; for ( int i = 1; i <= 10; i++) { printf("Please enter your weight: "); scanf("%f", &weight_value[i]); //Reads in the user's weight printf("Now enter your height: "); scanf("%f", &user_height[i]); //Reads in the user's height BMI[i] = weight_value / (user_height * user_height); } ... 

然后显示你需要一个像这样的循环

 ... printf("user \t weight \t height \t BMi"); for ( int i = 1; i <= 10; i++) { printf("%d \t%f \t%f \t%s ", i,user_weight[i],user_height[i],BMI[i]); } ... 

我只是重新阅读了这个问题并意识到我误读了,但最后一点显示了如何从数组中读取它们并显示它们。

以下代码将存储4个用户的权重和高度,并将其BMI打印为任意数量的阈值(在本例中设置为2)。

您可以通过将权重和高度存储到数组中来解决问题1.(在第一个循环中)。

然后,您可以通过首先定义一组阈值和相应的状态字符串来显示BMI状态字符串。 在第二个循环中,为您计算的每个用户检查BMI值是否低于给定阈值并打印其对应的状态字符串。

 #include  #include  #include  #define USERS 4 #define THRESHOLDS 2 int main(void) { float weight[USERS]; float height[USERS]; float BMI = 0.0f; char * BMI_Status[THRESHOLDS+1] = {"Normal","Overweight","Obese"}; float thresholds[THRESHOLDS] = {10.0,20.0}; int i,j; for ( i = 0; i < USERS; i++) { printf("Please enter your weight: "); scanf("%f", &(weight[i])); //Reads in the user's weight printf("Now enter your height: "); scanf("%f", &(height[i])); //Reads in the user's height } for ( i = 0; i < USERS; i++) { BMI = weight[i]/(height[i]*height[i]); printf("%d\t%f\t",i,BMI); for ( j = 0 ; j < THRESHOLDS ; j++ ){ if ( BMI < thresholds[j]){ break; } } printf("%s\n",BMI_Status[j]); } return 0; } 
  1. 在for循环中添加if语句
 float BMI[10]; int j; for ( i = 1; i <= 10; i++) { printf("Please enter your weight: "); scanf("%f", &weight_value); //Reads in the user's weight printf("Now enter your height: "); scanf("%f", &user_height); //Reads in the user's height BMI[i-1] = weight_value / (user_height * user_height); if (i==10) for (j=0; j<10; j++) printf ("%d\n", BMI[j]); } 
  1. 使用char **achar *a[]例如:
 char s[12] = "Hello World!"; char *p; p = &s[0]; char *BMI_Status[30]; //declare 30 char* BMI_Status[0] = "Fat"; BMI_Status[1] = "Thin"; ... //output printf ("%s", BMI_Status[0]); //Fat