为什么我的BMI平均代码无法正常工作?

时间:2013-10-24 01:02:49

标签: c#

我正在尝试计算一些人的平均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;

1 个答案:

答案 0 :(得分:7)

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

您需要添加:

float totalbmi;

然后经理计算应该是:

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