簡體   English   中英

check數組包含相同的數字

[英]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說沒有謂謂謂詞,我無法完全理解。

有另一種方式嗎?

有點卡在這里..希望有人可以幫助這個。

謂詞是一個方法,它將一個對象/變量作為參數,檢查一個條件並返回truefalse ..現在問題:

而不是做:

 if (Array.TrueForAll(throws, 6))

做:

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

但這是什么?

x => x == 6

正是我們所說的謂詞

是一個lambda,可以讀作:

在變量X中獲取數組中的每個元素。現在評估X == 6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM