C#使用变量和If语句创建方程式

时间:2019-04-04 18:21:58

标签: c# equation

我试图让用户输入他们的信息,该程序将获取该信息并找到您的BMI。我在方程式和if语句方面遇到麻烦。我似乎找不到我的问题所在。

 double _bmi = (_poundsVal / (_inchesVal * _heightVal)) * 703;


  if (_bmi <= 18.5)
  {
  Console.WriteLine("Your BMI is" + _bmi.ToString() + "you are considered underweight");
  }
  else if (_bmi > 18.5 && _bmi <= 24.9)
  {
  Console.WriteLine("Your BMI is " + _bmi.ToString() + "you are considered normal weight");
  }
  else if (_bmi <= 25 && _bmi <= 29.9)
  {
  Console.WriteLine("Your BMI is" + _bmi.ToString() + "you are considered overweight");
  }
  else 
  {
  Console.WriteLine("Your BMI is" + _bmi.ToString() + "you are considered obese");
  }

1 个答案:

答案 0 :(得分:1)

_bmi应该是double而不是int。更改此行:

int _bmi = (_poundsVal / (_inchesVal * _heightVal)) * 703;

double _bmi = (_poundsVal / (_inchesVal * _heightVal)) * 703;

其次,因为_bmi是双精度型,因此您无需解析它,因此如果保留并保留其内容,则将其删除:

if (double.TryParse(_bmi, out _bmiVal))