【发布时间】:2014-03-23 10:15:53
【问题描述】:
我对 do while 循环有疑问。当我这样做时。
它说:“再次”使用未分配的变量。我完全不知道为什么。
也许这是个愚蠢的问题..对不起,我刚开始学习如何编码,我对编程逻辑毫无感觉>
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");
}
}
}
【问题讨论】:
标签: c# console-application calculator