当我输入体重和身高时,c#bmi计算器NaN问题

时间:2015-03-02 05:55:23

标签: c#-2.0

我输入体重和身高,最终得到了BMI的结果。感谢您对此问题的帮助和考虑

double height = 0.0;
double weight = 0.0;
double bmi = height / weight;
double bmi = height / weight;
Console.Write("Enter your height in m:");
height = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter your weight in kg:");
weight = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Your BMI is:{0}", Math.Round(bmi,4));
bmi = Convert.ToDouble(Console.ReadLine());
Console.ReadKey();

1 个答案:

答案 0 :(得分:0)

您在分配bmiheight之前计算weight。它们的初始值为0,因此您有一个被零除的情况,这导致NaN:

double height = 0.0;
double weight = 0.0;
double bmi = height / weight;

bmi没有自动更新。您需要首先分配heightweight 然后计算bmi

Console.Write("Enter your height in m:");
double height = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter your weight in kg:");
double weight = Convert.ToDouble(Console.ReadLine());

double bmi = height / weight;
Console.WriteLine("Your BMI is:{0}", Math.Round(bmi,4));