我正在尝试制作一个简单的骰子游戏.它完全在控制台中.用户可以设置无限数量的骰子.然后游戏必须告诉所有骰子同时为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);
Run Code Online (Sandbox Code Playgroud)
但是,trueforall说没有谓谓谓词,我无法完全理解.
有另一种方式吗?
有点卡在这里..希望有人可以帮助这个.
谓词是一个方法,它接受一个对象/变量作为参数,检查一个条件并返回true或者false..现在问题:
而不是做:
if (Array.TrueForAll(throws, 6))
Run Code Online (Sandbox Code Playgroud)
做:
if (Array.TrueForAll(throws, x => x == 6))
Run Code Online (Sandbox Code Playgroud)
但这是什么?
x => x == 6
Run Code Online (Sandbox Code Playgroud)
正是我们所说的谓词
是一个lambda,可以读作:
在变量X中获取数组中的每个元素.现在评估X == 6