【问题标题】:C# Console Application do while loopC#控制台应用程序做while循环
【发布时间】: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


    【解决方案1】:

    如果您只关注代码的重要部分,您会得到:

    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 之外分配 another 的语句移动,如下所示:

    do {
      ...
      if (BMIOption == 1) {
        ...
      } else if (BMIOption == 2) {
        ...
      }
      again = Console.ReadLine();
    } while (again == "y" || again == "Y");
    

    您也可以使用其他人建议的值初始化 again 变量,但如果 BMIOption 不同于 1 或 2,则不会更新 again

    【讨论】:

      【解决方案2】:

      您的string again 未分配,并且您不能假设在do-while 循环中检查它时它总是有一个值。 只需将其初始化为任何值,例如string again="";

      【讨论】:

        【解决方案3】:

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

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

        您可能希望在声明时将其初始化为null

        string again = null;
        

        【讨论】:

          【解决方案4】:
          if (BMIOption == 1) {
          

          将再次设置。但是如果 is != 并且 is != 2 则永远不会再设置。 添加到 if 语句 un else 你再次设置或声明变量并赋值

          string again = string.Empty;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-01-14
            • 2017-03-22
            • 2017-03-14
            • 1970-01-01
            • 2016-05-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-07
            相关资源
            最近更新 更多