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

前端 未结 7 1820
情话喂你
情话喂你 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:27

    You can do the following

    var options = new Dictionary {
    {1, () => {
        //opt 1 code
    }},
    {2, () => {
        //opt 2 code
    }},
    {3, () => {
        //opt 3 code
    }},
    {4, () => {
        //opt 4 code
    }}
    };
    
    Console.WriteLine("Please enter you choice:");
    
    int P1Choice;
    while (!(int.TryParse(Console.ReadLine(), out P1Choice) && options.ContainsKey(P1Choice)))
    {
        Console.WriteLine("Input Invalid, Please press the number from the corresponding choices to try again:");
    }
    
    options[P1Choice]();
    

提交回复
热议问题