我如何可以在C拉这一关? [英] How can I pull this off in C?

查看:95
本文介绍了我如何可以在C拉这一关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序模拟BMI(身体质量指数)计算器,在输入需要从10个用户要求每个用户的体重和高度,然后程序计算出BMI并打印计算的结果格式化输出

I have a program simulating a BMI (Body Mass Index) Calculator that takes in inputs from 10 users asking each user for a weight and a height, the program then calculates the BMI and prints the result of the computation in a formatted output.

事情是这样的:

                  USER         BMI         STATUS
                  1
                  :
                  10

所以我想到这里,我将采取从用户至上的输入,然后尝试在上面格式化的方式来显示输入的计算。这就是我一直试图想出这使询问他们的体重和身高的用户,直到10日的用户。

So I thought of this, I would take in the inputs from the users first, then try to display the computation of the inputs in the above formatted way. Here's what I've tried to come up with which keeps asking the users for their weight and height until the 10th user.

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

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值是在给定范围内。

The BMI Status is a string that displays different states of the computed BMI values, which are "Underweight", "Normal", "Overweight" for when the BMI value is within a given range.

上面的code片段仍然是不完整的,我仍然发现很难添加code的其余部分。下面是我试图解决这个问题。

The code snippet above is still incomplete as I'm still finding it difficult to add the rest of the code. Here are the issues I'm trying to solve.


  1. 我怎样才能打印出来的BMI值,我已在10个输入后?

  1. How can I print out the BMI values after I have taken in the 10 inputs?

我曾尝试具有循环打印出的值的嵌套的,但是这是不符合逻辑的,因为现在是BMI为外循环的每一次迭代的输出,所以我猜需要一种方法来使 scanf()的持有BMI值之后的for循环的块。

I have tried having a nested for loop that prints out the values, but this is illogical as there will now be an output of the BMI for every iteration of the outer for loop, so I'm guessing need a way to make scanf() hold the BMI values after the for loop's block.

如何让我的状态字符串出现?

How do I make the status strings appear?

我知道有没有字符串数据类型在C,所以我必须声明,持有字符阵列串的状态,但这些是不同的状态不同的字符串,它有要打印的每行。我以为具有如果否则如果别人的条件,但我就是不能让字符阵列工作。

I know there's no string datatype in C, so I'll have to declare a char array that holds the string for the status, but these are different strings for different statuses, and it has to be printed out for every line. I thought of having if else if else conditions for this but I just can't get the char array to work.

我已经成功地写了一个code,在每次迭代打印出用户的BMI,但这并不是它如何应该做的。

I already successfully wrote a code that prints out the user's BMI at every iteration, but this isn't how its supposed to be done.

我想被保持BMI值在浮点数然后在上面格式化的方式排列显示值的数组,但我还是有点怀疑我打算怎么做这个的另一件事。

Another thing I thought of was keeping the BMI values in an array of floating point numbers then displaying the values from the array in the above formatted way, but I'm still a bit sceptical about how I intend doing this.

推荐答案

下面的完整code显示使用数组存储值。你的想法用其它如用于显示一个人是如何超重很好,所以我实现它。

Here the complete code to display the use of arrays to store the values. Your idea of using an if else for displaying how overweight a person is was good and so I implemented it.

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


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;
}

这篇关于我如何可以在C拉这一关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆