亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當一切看起來都很好時,條件不起作用

當一切看起來都很好時,條件不起作用

C#
森欄 2022-01-15 16:45:58
美好的一天,我的 Unity 游戲中有一個腳本,它創建一個列表并將其數字順序隨機化,然后將值傳遞給另一個列表以檢查一些特定屬性,這是代碼:// Use this for initializationprivate List<int> PreList = new List<int>();private List<int> ButtonList = new List<int>();private List<int> UserList = new List<int>();private System.Random Rnd = new System.Random();void Randomizer(){    PreList.Add(1);    PreList.Add(2);    PreList.Add(3);    PreList.Add(4);    PreList.Add(5);    PreList.Add(6);    PreList.Add(7);    PreList.Add(8);    PreList.Add(9);    PreList = PreList.OrderBy(C => Rnd.Next()).ToList();    foreach (int Number in PreList)    {         Debug.Log(Number);        Debug.Log(ButtonList.Count);        if (Number == 1)        {            OneMethod();        }        else if (Number == 2)        {            TwoMethod();        }        else if (Number == 3)        {            ThreeMethod();        }        else if (Number == 4)        {            FourMethod();        }        else if (Number == 5)        {            FiveMethod();        }        else if (Number == 6)        {            SixMethod();        }        else if (Number == 7)        {            SevenMethod();        }        else if (Number == 8)        {            EightMethod();        }        else if (Number == 9)        {            NineMethod();        }    }}    void OneMethod()    {        ButtonList.Add(1);        GameObject RedButton = GameObject.Find("Red"); //There are 8 methods just like this, but it variates some stuff like the name and the number, all of these add numbers to ButtonList    }此時,輸出控制臺只是說 ButtonList 的計數是 9,但是,如果我放一個 if 來檢查它,它永遠不會將值設為 true,就像它不執行方法并且 ifs 永遠不會運行,但是,你知道為什么嗎?
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

我不確定這是否會解決您的問題,但這是生成隨機順序列表的更好方法:


public class MyClass {

    private List<int> PreList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    private List<int> ButtonList = new List<int>();

    private List<int> UserList = new List<int>();


    void Randomizer() {

        while (PreList.Count > 0 ) {

            var idx = UnityEngine.Random.Range(0, PreList.Count); // Randomly select from remaining items

            var value = PreList[idx]; // Get item value

            PreList.RemoveAt(idx); // Remove item from future options

            ButtonList.Add(value); // Add to end of 'randomised' list

        }


        foreach (var value in ButtonList) {

            DoSomethingWith(value);

        }

    }


    void DoSomethingWith(int value) {

        switch(value) {

            case 1: OneMethod(); break;

            case 2: TwoMethod(); break;

            case 3: ThreeMethod(); break;

            case 4: FourMethod(); break;

            case 5: FiveMethod(); break;

            case 6: SixMethod(); break;

            case 7: SevenMethod(); break;

            case 8: EightMethod(); break;

            case 9: NineMethod(); break;

        }

    }

}

編輯:添加示例使用 DoSomething()


查看完整回答
反對 回復 2022-01-15
  • 1 回答
  • 0 關注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號