简体   繁体   English

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

[英]C# Console Application do while loop

I have an issue with do while loop. 我对do while循环有疑问。 When I do this. 当我这样做时。

it says :use of unassigned Variable "again". 它说:再次使用未分配的变量。 I totally have no idea why. 我完全不知道为什么。

Maybe its silly question..sorry i just start to learn how to code i have no sense of programing logic ><. 也许这是一个愚蠢的问题。对不起,我刚刚开始学习如何编码,我对编程逻辑> <没有任何了解。 Cheers 干杯

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

} }

If you concentrate only on important parts of your code you get this: 如果您只专注于代码的重要部分,则会得到以下信息:

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

As you can see if BMIOption is different from 1 or 2 then again will not be initialized. 如您所见,如果BMIOption不同于1或2,则不会again初始化。 You should most likely move the statement where you are assigning the again outside of ifs, like so: 您应该最有可能将语句移至在ifs之外再次分配语句的位置,如下所示:

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

You could also initialize the again variable with a value as others suggested, but then again won't be updated if BMIOption is different from 1 or 2. 你也可以初始化again变量和值等建议,但随后again将不会被更新,如果BMIOption是1或2个不同的。

That compiler errors comes from the fact that you are declaring a variable called again but you are not (always) assigning a value to it before it is used. 编译器错误的原因是,您声明了一个again调用的变量again但是在使用它之前(始终)没有为其赋值。

You'll need to decide what value again should have if you don't enter one of the if -blocks that assigns it. 如果您不输入分配它的if -blocks之一, if需要决定again应具有什么值。

You probably want to initialize it to null when you declare it: 您可能需要在声明时将其初始化为null

string again = null;

Your string again is not assigned and you can't assume it would always have a value when it's checked in the do-while loop. string again没有分配您的string again并且您无法假定在do-while循环中检查它时,它始终具有一个值。 Just initialize it to any value, like string again=""; 只需将其初始化为任何值,例如string again="";

if (BMIOption == 1) {

than again will be set. 比将再次设置。 but if is != and is != 2 then again is never set. 但如果是!=并且是!= 2,则永远不会再设置。 add to the if statement un else where you set again or declare variable and assign a value 将if语句添加到if语句中,在其他位置重新设置或声明变量并分配一个值

string again = string.Empty;

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

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