问题:要判断用户输入的一个数,或者是程序里方法的一个参数的值,或者是一个变量的值是否在某堆数中。

简洁写法:把这堆数放在list中,使用list的Contains()方法检查list是否包含这个数,取反。

代码如下:

       

 static void Main(string[] args)
        {
            Console.WriteLine("请输入一个数字:");
            string input=Console.ReadLine();
            if (!(new List<string>() { "3", "10", "11", "12", "19", "22", "51" }).Contains(input))  //Contains()包含,!取反
            {
                //这堆数不包含用户输入的数字,To do something 
                Console.WriteLine("不包含!");
               
            }
            Console.ReadKey();
        }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-03
  • 2021-05-23
  • 2021-12-16
  • 2021-11-05
  • 2022-12-23
  • 2021-12-31
  • 2021-10-09
相关资源
相似解决方案