0

我正在尝试计算一些人的平均 BMI。当我运行此代码时,每次单击计算按钮时,平均 BMI 都会下降。为什么?

float feet;
float inches;
float height;
float weight;
float bmi;
float averagebmi;

try
{
    //Get user input.
    feet = float.Parse(txtFeet.Text);
    inches = float.Parse(txtInches.Text);
    weight = float.Parse(txtWeight.Text);

    //Calculations.
    inches += feet * 12;
    height = inches * (float)0.0254;
    weight = weight * (float)0.453592;
    bmi = weight / (height * height);

    //Manager Calculations.
    totalPeople += 1;
    bmi += 1;
    averagebmi = bmi / totalPeople;
4

1 回答 1

7

平均 BMI 应该是总 BMI 除以总人数。

您需要添加:

float totalbmi;

那么经理的计算应该是:

//Manager Calculations.
totalPeople += 1;
totalbmi += bmi;
averagebmi = totalbmi / totalPeople;
于 2013-10-24T01:07:57.520 回答