2015-09-17 47 views
-2

我的程序設置爲讓用戶猜測1到10之間的一個整數。如果用戶猜測得太低或者太高,他們會收到通知並可以重試。在c中保留一個隨機數#

我遇到的問題是,當用戶猜測錯誤地產生一個新的隨機數。所以基本上,用戶在錯誤發生後從未嘗試過猜測相同的號碼。

我需要這樣做,以便當用戶猜錯時他們仍然試圖猜測相同的值。

這是我的代碼:

namespace IntegerGame 
{ 
    public partial class guessGame : Form 
    { 
     int num1; 
     int num2; 

     public guessGame() 
     { 
      InitializeComponent(); 
     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 
     } 

     private void guessButton_Click(object sender, EventArgs e) 
     { 
      Random rnd1 = new Random(); 
      num1 = rnd1.Next(1, 10); 

      if (int.TryParse(textBox1.Text, out num2)) 
      { 
       if (num2 < 0 || num2 > 10) 
       { 
        textBox1.Clear(); 
        MessageBox.Show("Please enter a number between 1 and 10"); 
       } 
       else 
       { 
        if (num2 > num1) 
        { 
         textBox1.Clear(); 
         MessageBox.Show("You guessed to high, please try again"); 
        } 

        else if (num2 < num1) 
        { 
         textBox1.Clear(); 
         MessageBox.Show("You guessed to low, please try again"); 
        } 

        else if (num2 == num1) 
        { 
         textBox1.Clear(); 
         MessageBox.Show("You guessed " + num2 + ", which was the right number!!"); 
        } 
       } 

      } 
      else 
      { 
       textBox1.Clear(); 
       MessageBox.Show("This is not a valid integer, please enter a valid integer"); 
      } 
     } 
    } 
} 
+5

提示:將您的一代移至其他方法。 –

+0

另一個提示與第一個一起:將'Random rnd1'移動到與'var num1'相同的地方 –

+0

您希望程序如何工作?像什麼時候應該生成一個新號碼?或者每次運行只有一次? – Michael

回答

7

生成隨機數作爲guessGame的成員(或在構造函數中,的InitializeComponent之後),而不是每當用戶按下按鈕

public partial class guessGame : Form 
{ 
    Random rnd1 = new Random(); 
    int num1 = rnd1.Next(1, 10); 
    int num2; 
    ... 
+1

謝謝,這有助於解決我的主要問題,但我仍然需要得到一個新的隨機生成後,用戶猜測正確。 –

+0

你想在你的情況下添加代。如果您不確定,請參閱下面的答案。 – Michael

+0

@BrianRoper只需使用rnd1.Next(1,10)獲取新的數字;在用戶猜對之後 –

3

每個的時間點擊按鈕,這個代碼是跑:

Random rnd1 = new Random(); 
num1 = rnd1.Next(1, 10); 

這意味着,everyt用戶猜測,這會產生一個新的隨機數。

我會建議把隨機和隨機數的字段(編輯:注意到您的號碼已經是一個場),對於最初的一家,在構造函數創建它,就像這樣:

private Random _rnd1; 
private int num1; 

GuessGame() 
{ 
    _rnd1 = new Random(); 
    _num1 = _rnd1.Next(1,10); 
    InitializeComponent(); 
} 

然後,當用戶猜測正確時,您可以通過將_num1字段設置爲隨機數中的下一個數字來簡單地生成一個新號碼。

1

檢查是否有需要生成數量,同時產生隨機數

Random rnd1 = new Random(); 
int num1 = -1; 

private void guessButton_Click(object sender, EventArgs e) 
{ 
    if (num1 == -1) 
    {    
     num1 = rnd1.Next(1, 10); 
    } 
    //... 

    //assign -1 to num1 after successful guess. 
    num1 = -1; 
} 
0

您應該設置一個隨機值,當窗體被初始化。它只會在表單關閉並重新打開後纔會更改。

Random rnd1 = new Random(); 

public guessGame() 
{ 
    InitializeComponent(); 
    num1 = rnd1.Next(1, 10); 
} 
0

剛剛產生的隨機數在構造函數中,而不是在單擊處理:

namespace IntegerGame 
{ 
public partial class guessGame : Form 
{ 
    int num1; 
    int num2; 

public guessGame() 
{ 
    Random rnd1 = new Random(); 
    num1 = rnd1.Next(1, 10); 
    InitializeComponent(); 
} 

private void textBox1_TextChanged(object sender, EventArgs e) 
{ 

} 

private void guessButton_Click(object sender, EventArgs e) 
{ 


    if (int.TryParse(textBox1.Text, out num2)) 
    { 


      if (num2 < 0 || num2 > 10) 
      { 
       textBox1.Clear(); 
       MessageBox.Show("Please enter a number between 1 and 10"); 
      } 

      else 
      { 
       if (num2 > num1) 
       { 
        textBox1.Clear(); 
        MessageBox.Show("You guessed to high, please try again"); 
       } 

       else if (num2 < num1) 
       { 
        textBox1.Clear(); 
        MessageBox.Show("You guessed to low, please try again"); 
       } 

       else if (num2 == num1) 
       { 
        textBox1.Clear(); 
        MessageBox.Show("You guessed " + num2 + ", which was the right number!!"); 
       } 
      } 

    } 

    else 
    { 
     textBox1.Clear(); 
     MessageBox.Show("This is not a valid integer, please enter a valid integer"); 
    } 




} 

} }

0
namespace IntegerGame 
{ 
public partial class guessGame : Form 
{ 
    int num1; 
    int num2; 
    Random rnd1 = new Random(); 

    public guessGame() 
    { 
     InitializeComponent(); 
     num1 = rnd1.Next(1, 10); 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void guessButton_Click(object sender, EventArgs e) 
    { 

     if (int.TryParse(textBox1.Text, out num2)) 
     { 


       if (num2 < 0 || num2 > 10) 
       { 
        textBox1.Clear(); 
        MessageBox.Show("Please enter a number between 1 and 10"); 
       } 

       else 
       { 
        if (num2 > num1) 
        { 
         textBox1.Clear(); 
         MessageBox.Show("You guessed to high, please try again"); 
        } 

        else if (num2 < num1) 
        { 
         textBox1.Clear(); 
         MessageBox.Show("You guessed to low, please try again"); 
        } 
        // Note that this could be an else: it is the only case left 
        else if (num2 == num1) 
        { 
         num1 = rnd1.Next(1, 10); 
         textBox1.Clear(); 
         MessageBox.Show("You guessed " + num2 + ", which was the right number!!"); 
        } 
       } 

     } 

     else 
     { 
      textBox1.Clear(); 
      MessageBox.Show("This is not a valid integer, please enter a valid integer"); 

}

要生成數只有當它們相等時,而不是每當按鈕被按下時。