繁体   English   中英

C#控制台应用程序执行while循环

[英]C# Console Application do while loop

我对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");
    }
}

}

如果您只专注于代码的重要部分,则会得到以下信息:

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将不会被更新,如果BMIOption是1或2个不同的。

编译器错误的原因是,您声明了一个again调用的变量again但是在使用它之前(始终)没有为其赋值。

如果您不输入分配它的if -blocks之一, if需要决定again应具有什么值。

您可能需要在声明时将其初始化为null

string again = null;

string again没有分配您的string again并且您无法假定在do-while循环中检查它时,它始终具有一个值。 只需将其初始化为任何值,例如string again="";

if (BMIOption == 1) {

比将再次设置。 但如果是!=并且是!= 2,则永远不会再设置。 将if语句添加到if语句中,在其他位置重新设置或声明变量并分配一个值

string again = string.Empty;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM