2016-08-10 20 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你絕對可以,也許你應該。 –

相關問題