有人可以帮我解决未分配的局部变量吗?

问题描述 投票:0回答:1

我目前正在为大学的编程课做一个项目,我快完成了。不幸的是,我的代码似乎有问题,我希望任何人都可以尽快修复它。如果您愿意的话,非常感谢。

我是一名初学者编码员,所以我对一切表示歉意。问题出在代码中显示

if (totalbmi <= 18.5m)
的行。

// Input Variables
decimal height;     // The height of the user
decimal weight;     // The weight of the user
decimal totalbmi;   // The total BMI after calculating the height and weight
string weightcategory = "YOU ARE:\n"; //Shows the user if they are overweight, underweight, or normal.
string gender = "YOUR GENDER IS: \n"; // Shows the user their gender after finishing calculations

// Validate that numeric information is in the textboxes.
if (decimal.TryParse(heightTextBox.Text, out height) && decimal.TryParse(weightTextBox.Text, out weight))
{
     totalbmi = (weight * 703) / (height * height); //This is the caulculation to find the total bmi.
     finalBMILabel.Text = totalbmi.ToString("n"); //This is where the final BMI will be placed after the calculation
}

// Tell the user their general weight category.
if (totalbmi <= 18.5m)
{
     weightcategory += "UNDERWEIGHT\n";
}
else if (totalbmi >= 18.5m && totalbmi <24.9m)
{
     weightcategory += "NORMAL WEIGHT\n";
}
else if (totalbmi >=25m && totalbmi < 29.9m)
{
     weightcategory += "OVERWEIGHT\n";
}

我尽力编辑变量类型并查看在线教程,但没有这样的运气。

c# variables
1个回答
0
投票

您已经定义了没有初始值的

totalbmi
,如果跳过第一个
if
块,当您检查它以确定向用户显示什么内容时,它仍然是未定义的。

有很多方法可以解决这个问题 - 这里有两种我会研究的方法:

(1) 只需在声明时将

totalbmi
变量预初始化为一个值,然后检查该预定义值来处理显示:

// Input Variables
decimal totalbmi = -1;   // initialize to "-1", signifying "undefined"

// Validate that numeric information is in the textboxes.
if (decimal.TryParse(heightTextBox.Text, out height) && decimal.TryParse(weightTextBox.Text, out weight))
{
     totalbmi = (weight * 703) / (height * height); //This is the caulculation to find the total bmi.
     finalBMILabel.Text = totalbmi.ToString("n"); //This is where the final BMI will be placed after the calculation
}

// check for "undefined"
if (totalbmi <= 0.0m)
{
    weightcategory += "UNDEFINED\n";
}
else if (totalbmi <= 18.5m)
{
     weightcategory += "UNDERWEIGHT\n";
}
.....

(2)

totalbmi
计算结果的显示移动到同一个块中,您可以确保在其中有一个值:

// Input Variables
decimal totalbmi = -1;   // initialize to "-1", signifying "undefined"

// Validate that numeric information is in the textboxes.
if (decimal.TryParse(heightTextBox.Text, out height) && decimal.TryParse(weightTextBox.Text, out weight))
{
    totalbmi = (weight * 703) / (height * height); //This is the caulculation to find the total bmi.
    finalBMILabel.Text = totalbmi.ToString("n"); //This is where the final BMI will be placed after the calculation
     
    // Tell the user their general weight category.
    if (totalbmi <= 18.5m)
    {
         weightcategory += "UNDERWEIGHT\n";
    }
    else if (totalbmi >= 18.5m && totalbmi <24.9m)
    {
         weightcategory += "NORMAL WEIGHT\n";
    }
    else if (totalbmi >=25m && totalbmi < 29.9m)
    {
         weightcategory += "OVERWEIGHT\n";
    }
}
else 
{
    Console.WriteLine("Cannot calculate total BMI");
}
© www.soinside.com 2019 - 2024. All rights reserved.