【问题标题】:C# array for finding all the 2's用于查找所有 2 的 C# 数组
【发布时间】:2016-03-20 14:58:15
【问题描述】:

基本上我要做的是找到一个特定的数字,在这种情况下是 2,然后看看我在我的程序中有多少次使用这个数字,我假设我必须使用 .GetValue(42) 但是它做得不对,我使用的代码是

static int count2(int[] input)
{
    return input.GetValue(2);

}

输入来自一个单独的方法,但它包含我正在使用的值

int [] input = {1,2,3,4,5};

【问题讨论】:

  • 有几种方法可以做到这一点.. 除了您发布的内容之外,您还尝试过什么..?这并不难..
  • 我搞砸了 For(;;) 代码,但它根本不起作用,我真的是新手,以前从未见过。

标签: c# arrays loops numbers


【解决方案1】:

不确定你是专门计算数字 2 还是包含数字 2 的任何数字。

对于后者,这里是简单的方法:

public int count2(int[] input) {
    int counter = 0;
    foreach(var i in input) {
        if (i.ToString().Contains("2"))
        {
            ++counter;
        }
    }
    return counter;
}

【讨论】:

    【解决方案2】:

    你可以用 LINQ 做到这一点 input.Count(x=>x==2);

    【讨论】:

    • 这将计算一个正好是 2 的输入,不确定这是否是 OP 的意思。
    【解决方案3】:

    Array.GetValue()“获取一维数组中指定位置的值”这不是你想要的。 (在您的示例中,它将返回 3,因为这是数组索引 2 处的值)。

    您想计算特定项目在数组中 的次数。这是循环和检查每个项目的问题:

    var counter = 0;
    foreach(var item in input)
    {
        if(item == 2)
        {
            counter++;
        }
    }
    
    return counter;
    

    【讨论】:

      【解决方案4】:

      要计数,请执行此操作

         int [] inputDupes = {1,2,3,4,5,2};
         var duplicates = inputDupes
                 .Select(w => inputDupes.Contains(2))
                 .GroupBy(q => q)
                 .Where(gb => gb.Count() > 1)
                 .Select(gb => gb.Key).Count();//returns an Int32 value
      

      查看是否有重复的数字 2 然后执行以下操作

         int [] inputDupes = {1,2,3,4,5,2};
         var duplicates = inputDupes
                 .Select(w => inputDupes.Contains(2))
                 .GroupBy(q => q)
                 .Where(gb => gb.Count() > 1)
                 .Select(gb => gb.Key)
                 .ToList(); //returns true | false
      

      如果您想根据任何数字执行此操作,则创建一个方法并在调用 .Contains() 扩展方法的位置传递一个参数

      如果您想从控制台捕获用户输入,您也可以这样做

         int [] inputDupes = {1,2,3,4,5,2};
         Console.WriteLine("Enter a number to check for duplicates: ");
         string input = Console.ReadLine();
         int number;
         Int32.TryParse(input, out number);
         var dupeCount = inputDupes.Count(x => x == number);
         Console.WriteLine(dupeCount);
         Console.Read();
      

      重复次数为 2

      【讨论】:

      • 为什么要在已经是IEnumerable的数组上调用.AsEnumerable()
      【解决方案5】:
      static int count2(int[] input)
      {
          return input.Count(i => i == 2);
      }
      

      【讨论】:

        【解决方案6】:

        你可以像这样使用 Func:

        public Func<int[], int, int> GetNumberCount =
          (numbers, numberToSearchFor) => 
          numbers.Count(num => num.Equals(numberToSearchFor));
        
        ...
        
        var count = GetNumberCount(input, 2);
        

        一定要喜欢 Func :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多