2014-09-21 27 views
-4

我很新,我想知道如果我按下一個隨機數字但是當這個數字是2時應該有另一個文本框顯示它有多少次2 。c#當數字是6時數字+1 +1

不使用的if/else或切換

private void btnChoose_Click(object sender, EventArgs e) 
{ 
     double number2 = 0; 
     double When2 = 2; 

     Random number = new Random(); 
     double chose = number.Next(1,7); 
     txtnumber.Text = chose.ToString(); 

     txtnumber2.Text = something... 
} 

解決方案:(它不可能用數字2,但它是與數字6)

// global var 
    int number6 = 0; 
    private void btnChoose_Click(object sender, EventArgs e) 
    { 

      double When6 = 6; 

      Random number = new Random(); 
      double chose = number.Next(1,7); 
      txtnumber.Text = chose.ToString(); 
      number6 = Convert.toInt32(number6 + (chose/When6)); 

      txtnumber6.Text = number6.ToString(); 
    } 

LB注:OP的在被刪除的答案

這是一個非常是煩人的任務,我只需要做到這一點沒有,如果/ ESE或切換

的一個評論
+1

這是功課嗎? http://stackoverflow.com/questions/25962571/problems-reading-and-writing-to-a-text-file-in-c-sharp – 2014-09-21 19:22:50

+0

'txtnumber2.Text =選擇== 2? (++計數器).ToString():計數器。ToString()' – 2014-09-21 19:32:44

+0

爲什麼downvote?這個問題**沒有if/else **很好 – 2014-09-21 19:40:57

回答

6

沒有數組,沒有字典,沒有LINQ中,沒有內置的方法,如Convert.ToIn32等,只有位操作

Random number = new Random(); 
int TwosCount = 0; 

private void btnChoose_Click(object sender, EventArgs e) 
{ 
    int chose = number.Next(1, 7); 
    TwosCount += ~((chose & 4) >> 2) & ((chose & 2) >> 1) & ~((chose & 1) >> 0); 

    txtnumber.Text = chose.ToString(); 
    txtnumber2.Text = TwosCount.ToString(); 
} 

PS:你可以刪除一些不必要的括號並寫爲:

~(chose & 4) >> 2 & (chose & 2) >> 1 & ~(chose & 1); 
0
Random R = new Random(); 
void RUN() 
{ 
    int N = R.Next(2); 
    Action[] A = new Action[2]; 
    A[0] = new Action(Act1); 
    A[1] = new Action(Act2); 

    A[N].Invoke(); 
} 

private void Act2() 
{ 
    MessageBox.Show("Cancel"); 
} 

private void Act1() 
{ 
    MessageBox.Show("OK"); 
} 

這個答案是沒有的if/else /開關

3

對於像這樣的隨機數的小範圍,(Random.Next(1,7)產生從1到6的數字),您可以使用一個數組來跟蹤數字出現的次數。確保在你的主類中聲明這個數組,但不在按鈕單擊方法中。另外,它看起來並不需要爲這個工作使用浮點數,所以我將所有的雙變量都改爲整數。

int[] numberCount = new int[6]; 
//if you really want to stay with doubles, then simply change the above line to this: 
//double[] numberCount = new double[6]; 

private void btnChoose_Click(object sender, EventArgs e) 
{ 
    int number2 = 0; 
    //double When2 = 2; This line is not needed, so it's been commented out. 

    Random number = new Random(); 
    int chose = number.Next(1,7); 
    numberCount[chose - 1]++; // Increment the amount of times we've seen this particular number 
    txtnumber.Text = chose.ToString(); 

    txtnumber2.Text = numberCount[1].ToString(); // Display the amount of times we've seen the number 2 
    //Remember, array indexes are 0 based, so index 1 is actually the second number in the array, or number 2. 
} 
+0

@TimSchmelter使用詞典更具可擴展性。 – brz 2014-09-21 20:32:37

2

我們可以用這個公式來找到我們的索引(Rnd -3)%2

當我們從價值減去3,如果2或更低,它會得到負面結果

1-3 = -2和-2%2 = 0

2-3 = -1和-1%2 = -1

和其它數量的結果將是0或1。

然後我們可以將計算值加1以從0開始索引,現在我們有一個包含三個元素的數組,其中第一個元素是出現次數爲。

private readonly Random _random=new Random(); 
    private readonly int [] numbers=new int[3]; 

    private void button1_Click(object sender, EventArgs e) 
    { 
     var number = _random.Next(1,7); 
     var chose = number; 
     chose -= 3; //mod (2-3)=-1 
     chose %= 2; 
     chose++; 

     numbers[chose]++; 

     txtnumber.Text = number.ToString(); 
     txtNumber2.Text = numbers[0].ToString(); 
    } 
1

也許;-)

double[] two = { 2 }; 
int count2 = 0; 

private void btnChoose_Click(object sender, EventArgs e) 
{ 
    double chose = number.Next(1, 7); 
    txtnumber.Text = chose.ToString(); 
    count2 = count2 + System.Convert.ToInt32(two.Contains(chose)); 
    txtnumber2.Text = count2.ToString(); 
} 
+1

這是迄今爲止提出的最好的解決方案,它可以同時使用自然數和真實數。與數組或字典解決方案不同,它不會使用大量內存以適應更廣泛的隨機範圍,我想知道爲什麼費率會提供其他方法! – user3473830 2014-09-21 20:45:00