我遇到了do while循环的问题。当我这样做。
它说:使用未分配的变量"再次"。我完全不知道为什么。
也许是愚蠢的问题......我只是开始学习如何编码我没有编程逻辑>&lt ;.干杯
static void Main(string[] args) {
double WeightKg = 0.0, HeightCm = 0.0, Weightlbs = 0.0, WeightOz = 0.0, BMI = 0.0, Feet = 0.0, Inches = 0.0;
int BMIOption;
string again;
do{
string BMIMenu = ("Which Measurement You Want to use to enter the weight and height?"
+ "\n1)Enter 1 for Metric"
+ "\n2)Enter 2 for British Imperial:");
Console.Write(BMIMenu);
BMIOption = int.Parse(Console.ReadLine());
if (BMIOption == 1) {
Console.Write("\nPlease Enter your Weight in Kilogram (kg):");
WeightKg = double.Parse(Console.ReadLine());
Console.Write("\nPlease Enter your Height in in centimetres (cm):");
HeightCm = double.Parse(Console.ReadLine());
BMI = WeightKg / (HeightCm / 100 * HeightCm / 100);
if (BMI >= 35.0) {
Console.WriteLine("\nYour BMI is {0:G},Severe Obesity", BMI);
} else if (BMI >= 30.0) {
Console.WriteLine("\nYour BMI is {0:G},Obese", BMI);
} else if (BMI >= 25.0) {
Console.WriteLine("\nYour BMI is {0:G},OverWeight", BMI);
} else if (BMI >= 18.5) {
Console.WriteLine("\nYour BMI is {0:G},Healthy BodyWeight", BMI);
} else if (BMI <= 18.5) {
Console.WriteLine("\nYour BMI is {0:G},UnderWeight", BMI);
}//End if
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
again = Console.ReadLine();
} else if (BMIOption == 2) {
Console.WriteLine("Please Enter your Weight in Pounds (lbs):");
Weightlbs = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter your Weight in Ounces (oz):");
WeightOz = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter your Height in Feet (ft):");
Feet = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter your Height in Inches (ins):");
Inches = double.Parse(Console.ReadLine());
WeightKg = ((Weightlbs * 16) + WeightOz) / 35.2;
HeightCm = ((Feet * 12) + Inches) * 2.54;
BMI = WeightKg / (HeightCm / 100 * HeightCm / 100);
if (BMI >= 35) {
Console.WriteLine("Your BMI is {0:G},Severe Obesity", BMI);
} else if (BMI >= 30) {
Console.WriteLine("Your BMI is {0:G},Obese", BMI);
} else if (BMI >= 25) {
Console.WriteLine("Your BMI is {0:G},OverWeight", BMI);
} else if (BMI >= 18.5) {
Console.WriteLine("Your BMI is {0:G},Healthy BodyWeight", BMI);
} else if (BMI <= 18.5) {
Console.WriteLine("Your BMI is {0:G},UnderWeight", BMI);
}//End if
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
again = Console.ReadLine();
}
} while (again == "y" || again == "Y");
}
}
}
答案 0 :(得分:3)
如果你只专注于代码的重要部分,你就会得到:
string again;
do {
...
if (BMIOption == 1) {
...
again = Console.ReadLine();
} else if (BMIOption == 2) {
...
again = Console.ReadLine();
}
} while (again == "y" || again == "Y");
如您所见,BMIOption
与1或2不同,则again
将不会被初始化。您最有可能将语句移动到ifs以外的地方,如下所示:
do {
...
if (BMIOption == 1) {
...
} else if (BMIOption == 2) {
...
}
again = Console.ReadLine();
} while (again == "y" || again == "Y");
您还可以使用其他人建议的值初始化again
变量,但如果again
与1或2不同,则BMIOption
不会更新。
答案 1 :(得分:0)
编译器错误来自于您声明一个名为again
的变量这一事实,但您在使用之前并未(始终)为其分配值。
如果您没有输入分配给它的again
块之一,您需要决定if
应该具有的值。
您可能希望在声明它时将其初始化为null
:
string again = null;
答案 2 :(得分:0)
您的string again
未分配,并且您不能认为在do-while循环中检查它时将始终具有值。
只需将其初始化为任何值,例如string again="";
答案 3 :(得分:0)
if (BMIOption == 1) {
将再次设定。但如果是!=并且是!= 2那么再也没有设置。 在你再次设置的地方添加if语句,或者声明变量并赋值
string again = string.Empty;