如何从Arraylist或随机数中搜索多个数字?

时间:2016-08-10 19:45:18

标签: c#

我有一个包含10个数字的列表。 当我单击按钮时,如果CheckBox +数字

//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?

}

2 个答案:

答案 0 :(得分:1)

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

// 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) {
  ...
}

答案 1 :(得分:0)

请尝试以下方法。希望它有所帮助:

        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)
            {
            }
        }