2014-10-30 48 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的答案滾入一條線。請注意,我絕不會在生產代碼中寫這樣的東西,而是其他人建議的東西。

相關問題