这是一个BMI计算器,可根据用户选择的选项计算英制或公制BMI。然后我需要显示他们的BMI状态(体重不足,超重,肥胖)。我在Person类中有一个方法,我想在main方法中调用,但是Visual Studio强调了bmi变量,并说"使用未分配的局部变量"
如何修复变量问题?
这是类代码:
class Person
{
double Height { get; set; }
double Weight { get; set; }
double Inches { get; set; }
double Bmi { get; set; }
public Person()
{
}
public Person(double height, double weight, double bmi)
{
Height = height;
Weight = weight;
Bmi = bmi;
}
public Person (double height, double weight, double inches, double bmi)
{
Height = height;
Weight = weight;
Inches = inches;
Bmi = bmi;
}
//calculates BMI using metric formula
public static double CalcMetric(double height, double weight)
{
double bmi = weight / Math.Pow(height, 2);
return bmi;
}
//calculates BMI using imperial formula
public static double CalcImperial(double height, double weight)
{
double bmi = weight / Math.Pow(height, 2) * 703;
return bmi;
}
public void Status(double bmi)
{
if (bmi < 18.5)
{
Console.WriteLine("Your BMI status is underweight.");
}
else
if (bmi < 24.9)
{
Console.WriteLine("Your BMI status is normal.");
}
else
if (bmi < 29.9)
{
Console.WriteLine("Your BMI status is overweight.");
}
else
if (bmi > 30)
{
Console.WriteLine("Your BMI status is obese.");
}
}
}
}
这是主程序
class Program
{
static void Main(string[] args)
{
Console.BackgroundColor = ConsoleColor.White; //set background
Console.Clear();
Console.ForegroundColor = ConsoleColor.Black; //set foreground
string inputHeight;
string inputWeight;
string aValue;
double choice;
double height;
double weight;
double bmi;
Person p = new Person();
Console.WriteLine("Welcome to BMI Calculator. \nEnter 1 for imperial calculator, 2 for metric calculator or 3 to quit.");
aValue = Console.ReadLine();
choice = double.Parse(aValue);
if (choice == 1)
{
Console.WriteLine("Enter height in inches: ");
inputHeight = Console.ReadLine();
Console.WriteLine("Enter weight in lbs: ");
inputWeight = Console.ReadLine();
height = double.Parse(inputHeight);
weight = double.Parse(inputWeight);
Console.WriteLine("Your BMI is: " + Person.CalcImperial(height, weight));
}
else
if (choice == 2)
{
Console.WriteLine("Enter height in meters: ");
inputHeight = Console.ReadLine();
Console.WriteLine("Enter weight in kilos: ");
inputWeight = Console.ReadLine();
height = double.Parse(inputHeight);
weight = double.Parse(inputWeight);
Console.WriteLine("Your BMI is: " + Person.CalcMetric(height, weight));
}
else
if (choice == 3)
{
Console.WriteLine("Goodbye!");
}
p.Status(bmi);
Console.ReadKey();
}
}
答案 0 :(得分:1)
您收到错误,因为double bmi;
未初始化。根据引导线方法,成员需要在方法中初始化,您不能只声明它。所以你需要这样做
double bmi=0;
检查一下:Are C# uninitialized variables dangerous?
您的代码还有一个问题,
//in your person class
public void Status(double bmi)
{
}
你正在做p.Status(ref bmi);
,你不会得到更新的值,因为double是在函数中完成的值类型更改而不是反映实际值,你需要这样做,
//in your person class
public void Status(ref double bmi)
{
}
在参考的帮助下,你将从函数中获得价值。
更好的解决方案是从函数返回值
//in your person class
public double Status()
{
double bmi=0;
//rest of code
return bmi;
}
主bmi = p.Status();
中的
答案 1 :(得分:0)
Bmi是人类的内部成员(默认情况下),因此您不能直接使用BMI而是使用
Status(CalcMetric); or `status(CalcImperial);` *according to your requirement*
这样这些函数在函数调用期间返回BMI的值。