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

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

    I hope this is what you need. The possibles values are in the List "list" and it loops until the answer is one of the possible values:

            int value = 0;
    
            List list = new List { 1, 2, 3, 4 }; // choices are in the list
    
            while (true)
            {
                Console.WriteLine("Please enter a number :");
                if (int.TryParse(Console.ReadLine(), out value))
                {
                    if (list.Contains(value))
                        break;
                }
            }
    
            // value is in the list, deal with it.
    

提交回复
热议问题