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

C# create an equation with Variables and If statement

我正在尝试让用户输入他们的信息,程序将获取这些信息并找到您的 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");
  }

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

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

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

其次,因为 _bmi 是一个双精度数,你不需要解析它,所以删除它,只保留它的内容:

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