2016-08-10 18 views
-3

我有一个10个数字的列表。 当我点击按钮后,如果您想将CheckBox +数量我如何从一个Arraylist或随机数中搜索多个数字?

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

} 
+0

那么....你的问题是什么? –

+3

if(checkBox1to5.Checked &&(theNumber> = 1 && theNumber <= 5)) – Kevin

+0

@Kevin实际上并不需要额外的缺陷,尽管 – Dispersia

回答

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

你可以只做checkBox1to5.Checked而不是让它们等于true – Jetti

+0

@Jetti你绝对可以,也许你应该。 –

相关问题