How do I create a terminable while loop in console application?

前端 未结 7 1817
情话喂你
情话喂你 2021-01-16 04:41

I am currently looking for a solution for this c# console application function

I tried searching for a method for creating a while loop that can terminate for the co

7条回答
  •  -上瘾入骨i
    2021-01-16 05:43

    You just have to use readline inside your while loop and in else also do break. It should work this way:

     int P1Choice;
    
        while (true)
        {
        P1Choice = int.Parse(Console.ReadLine());
    
            if (P1Choice == 1)
            {
                Console.WriteLine("");
                CenterWrite("You have chosen Defult Empire 1");
                break;
            }
            if (P1Choice == 2)
            {
                Console.WriteLine("");
                CenterWrite("You have chosen Defult Empire 2");
                break;
            }
            if (P1Choice == 3)
            {
                Console.WriteLine("");
                CenterWrite("You have chosen Defult Empire 3");
                break;
            }
            if (P1Choice == 4)
            {
                Console.WriteLine("");
                CenterWrite("You have chosen Defult Empire 4");
                break;
            }
            else
            {
                Console.WriteLine("");
                CenterWrite("Input Invalid, Please press the number from the corresponding choices to try again");
        break;
            }
        }
    

提交回复
热议问题