检查数组包含相同的数字

check array contains the same numbers

我正在尝试制作一个简单的骰子游戏。它完全在控制台中。 用户可以设置无限数量的骰子。然后游戏必须告诉所有骰子同时为 6 需要掷多少次。

我试过这样的,

    int i = 0;
    int[] throws = new int[4000];
    bool success = false;
    do
    {
        throws[1] = dice.Next(1, 7);
        throws[2] = dice.Next(1, 7);
        throws[3] = dice.Next(1, 7);
        throws[4] = dice.Next(1, 7);
        throws[5] = dice.Next(1, 7);
        throws[6] = dice.Next(1, 7);

        if (Array.TrueForAll(throws, 6))
        {
            success = true;
        }
        i++;
    } while (success != true);

但是 trueforall 说失败了谓词,我一直无法完全理解。

还有其他方法吗?

有点卡在这里..希望有人能帮忙解决这个问题。

谓词是一种方法,它以一个 object/variable 作为参数,检查该参数的条件,然后 returns truefalse.. 现在讨论这个问题:

而不是做:

 if (Array.TrueForAll(throws, 6))

做:

 if (Array.TrueForAll(throws, x => x == 6))

但是这是什么?

x => x == 6

正是我们正在谈论的谓词

是一个 lambda,可以读作:

take every element in the array, in a variable X. now evaluate if X == 6