如何从Arraylist或Random Number中查询多个数字

本文关键字:查询 数字 Number Random Arraylist | 更新日期: 2023-09-27 18:01:42

我有一个10个数字的列表。当我点击按钮时,如果复选框+数字

//form.cs
Random rnd = new Random();
int theNumber = rnd.Next(1,11);
if (checkBox1to5.Checked == true && theNumber == 1 || theNumber == 2...)
{
    //What is the more simple way to code this?
}
elseif (checkBox6to10.Checked == true && theNumber == 6 || theNumber == 7...)
{
    //AND also, would it be any different if i was searching the number from a Array List, rather then a random generated number?
}

如何从Arraylist或Random Number中查询多个数字

如果你只想简化你的代码:

// do not re-create Random, it can make sequence being badly skewed
// create Random just once
private static Random rnd = new Random();
...
int theNumber = rnd.Next(1, 11);
if (checkBox1to5.Checked && theNumber <= 5) {
  ...
}
else if (checkBox6to10.Checked && theNumber >= 6) {
  ...
}

请尝试以下操作。希望能有所帮助:

        Random rnd = new Random();
        int theNumber = rnd.Next(1,11);
        int[] intarray = {5, 6, 7, 8}
        if (checkBox1to5.Checked == true && theNumber > 0 && theNumber < 6 )
        {
        }
        else if (checkBox6to10.Checked == true && theNumber > 5 && theNumber < 12)
        {
        }
        // For array List
        foreach(int num in intarray)
        {
            if (checkBox1to5.Checked == true && num > 0 && num < 6 )
            {
            }
            else if (checkBox6to10.Checked == true && num > 5 && num < 12)
            {
            }
        }