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

前端 未结 7 1816
情话喂你
情话喂你 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条回答
  • 2021-01-16 05:43

    One option can be to create method and keep calling until valid input comes:

    public static void ProcessInput(string input)
    {
        int choice = Convert.ToInt32(input);
        switch (choice)
        {
            case 1:
                Console.WriteLine("");
                Console.WriteLine("You have chosen Defult Empire 1");
                break;
            case 2:
                Console.WriteLine("");
                Console.WriteLine("You have chosen Defult Empire 2");
                break;
            case 3:
                Console.WriteLine("");
                Console.WriteLine("You have chosen Defult Empire 3");
                break;
            case 4:
                Console.WriteLine("");
                Console.WriteLine("You have chosen Defult Empire 4");
                break;
            default:
                Console.WriteLine("");
                Console.WriteLine("Input Invalid, Please press the number from the corresponding choices to try again");
                ProcessInput(Console.ReadLine());
                break;
        }
    

    and in your main program:

    public static void Main()
    {
        Console.WriteLine("Hello World");
        ProcessInput(Console.ReadKey());
    }
    
    0 讨论(0)
提交回复
热议问题