gpt4 book ai didi

c# - 检查数组包含相同的数字

转载 作者:行者123 更新时间:2023-11-30 19:36:29 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的骰子游戏。它完全在控制台中。用户可以设置无限数量的骰子。然后游戏必须告诉所有骰子同时为 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 说失败了一些叫做 predicate 的东西,我一直无法完全理解它。

还有别的办法吗?

有点卡在这里..希望有人能帮忙。

最佳答案

谓词是一种方法,它接受一个对象/变量作为参数,检查一个条件并返回 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

关于c# - 检查数组包含相同的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45561918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com