2014-10-30 41 views
0

我有一个循环,打印出不同的BMI值10级的用户在此格式:C - 如何为评估表达式的结果输出不同的字符串?

     USER BMI STATUS 
         1 
         : 
         10 

的BMI状态最终字符串"Overweight", "Normal" , "Underweight", and "Obese"

  1. 减持 - 当BMI低于18.50
  2. 正常 - 当BMI从18.50到24.9时
  3. 超重 - 当BMI从25.0到29.9时
  4. 肥胖 - 当BMI是大于30.0

我已经想通了如何打印出从真棒答案我here新线的BMI值,但我仍然发现很难显示BMI状态。

这里是我有,

#include <stdio.h> 
#include <math.h> 

int main(void) { 

    float weight_value[10]; 
    float user_height[10]; 
    float BMI = 0.0f; 
    char* BMI_Status[] = {"Underweight", "Normal", "Overweight", "Obese"}; 

    int i; 

    for (i = 0; i < 10; i++) 
    { 
     printf("Please enter your weight: "); 
     scanf("%f", &weight_value[i]); //Reads in the user's weight and stores it in an array 
     printf("Now enter your height: "); 
     scanf("%f", &user_height[i]); //Reads in the user's height and stores it in an array. 
    } 
    printf("\n"); 
    printf("USER     BMI     STATUS\n"); 

    for (i = 0; i < 10; i++) 
    { 
     BMI = weight_value[i]/(user_height[i] * user_height[i]); 
     printf("%2d %23.7f \n", i+1 , BMI); 
    } 

    return 0; 
} 

我创建的字符数组保存的BMI状态字符串,但因为这些都是将要在代码的表达推断串,我不知道如何在每一行上打印它们。

我想过使用if条件来测试BMI值何时为真,但是当我到达要添加打印出BMI状态字符串的参数的部分时,我感到困惑。

+0

'if'有什么问题?你可以在得到'BMI'的值后继续' – 2014-10-30 14:06:59

+0

@SouravGhosh我尝试过这样做,但是我发现在第二次循环的每次迭代时都难以打印出不同的'BMI_Status []'字符串。 – 2014-10-30 14:09:59

+2

考虑一个变量标志,如果bmi <18.50,do标志= 0,否则如果18.50 2014-10-30 14:13:54

回答

0

要根据计算出的BMI打印状态,您需要选择保存状态值的阵列的正确索引。您可以使用单独的变量来实现相同的功能。检查下面的代码来获得一个想法。

#include <stdio.h> 
#include <math.h> 

int main(void) { 

     float weight_value[10]; 
     float user_height[10]; 
     float BMI = 0.0f; 
     char* BMI_Status[] = {"Underweight", "Normal", "Overweight", "Obese"}; 

     int i; 

     int flag = -1; //used as index 

     for (i = 0; i < 10; i++) 
     { 
       printf("Please enter your weight: "); 
       scanf("%f", &weight_value[i]); //Reads in the user's weight and stores it in an array 
       printf("Now enter your height: "); 
       scanf("%f", &user_height[i]); //Reads in the user's height and stores it in an array. 
     } 
     printf("\n"); 
     printf("USER     BMI     STATUS\n"); 

     for (i = 0; i < 10; i++) 
     { 
       BMI = weight_value[i]/(user_height[i] * user_height[i]); 
       //after getting the BMI value, select the string index to be printed 
       if (BMI < 18.5) 
       { 
           flag = 0; 
       } 
       else if ((BMI > 18.5) && (BMI < 24.9)) 
       { 
           flag = 1; 
       } 
       else if ((BMI > 24.9) && (BMI < 29.9)) 
       { 
           flag = 2; 
       } 
       else if (BMI > 30) 
       { 
           flag = 3; 
       } 

       printf("%2d %23.7f \t\t%s\n", i+1 , BMI, BMI_Status[flag]); //print the stats 
     } 

     return 0; 
} 

注意:您可能需要添加一些检查来验证数据。

+0

亲爱的Downvoter,有何评论? – 2014-10-31 13:48:38

2

如果你真的需要在同一行的每一件事情,你可以写所有三级运营商表现的母亲:

printf("%2d %23.7f %23s", i+1, BMI, BMI_Status[(BMI<18.5 ? 0 : (BMI < 24.9 ? 1 : (BMI < 29.9 ? 2 : 3)))]) 

在现实中,这仅仅是如果-ELSEIF-ELSE风格乔M的答案滚入一条线。请注意,我绝不会在生产代码中写这样的东西,而是其他人建议的东西。

相关问题