小编Yas*_*een的帖子

如何检查数字数组中出现的具体数字

我想检查一组数字中出现的具体数字.例如,数字2出现:

input from 1 to 20 
output
3 times 
Run Code Online (Sandbox Code Playgroud)

另一个例子:

input from 1 to 100
output 
19 times 
Run Code Online (Sandbox Code Playgroud)

这是我的代码

int count = 0;
string x = "";
string y = "";
string[] arr2 = new string[100000000];
for (int i = 1; i < arr2.Length; i++)
{
    arr2[i - 1] = i.ToString();
}

foreach (var item in arr2)
{  
    for (int digit = 0;digit<item.Length;digit++)
    {
        if (digit == 2)
            count++;
    }
}
Console.WriteLine(count);
Run Code Online (Sandbox Code Playgroud)

我的代码不起作用,但我不知道问题出在哪里.

注意:这必须仅使用for/ foreachloops,而不是使用Dictionary或LINQ或 …

c# arrays

-1
推荐指数
1
解决办法
84
查看次数

标签 统计

arrays ×1

c# ×1