2014-10-11 120 views
2

因此,我正在爲一個在1-100之間隨機生成的數字在猜謎遊戲中的學校項目工作。目前我唯一遇到的問題是,每當用戶輸入一個新號碼時,生成的號碼也會改變。我試着將代碼放在表單加載器中生成數字,但後來在程序中無法訪問它。這是迄今爲止我所擁有的。在c中的隨機數猜謎遊戲的麻煩#

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApplication5 
{ 

    public partial class Form1 : Form 
    { 
     public Form1() 
     { 

      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void guessButton_Click(object sender, EventArgs e) 
     { 

      int userGuess; 
      userGuess = int.Parse(guessText.Text); 
      Random rand = new Random(); 
      int number = rand.Next(1, 100); 

       label2.Text = "" + number; 


        if (userGuess > number) 
        { 
         resultLabel.Text = "Your guess is too high"; 
        } 

        else if (userGuess < number) 
        { 
         resultLabel.Text = "Your guess is too low."; 
        } 

      else if (userGuess == number) 
      { 
       resultLabel.Text = "That is correct!"; 
      } 

       guessText.Clear(); 



     } 

     private void exitButton_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 
    } 
} 
+0

http://csharpindepth.com/Articles/Chapter12/Random.aspx – Oded 2014-10-11 19:11:15

+0

形式裝載機在正確的位置把它。查找哪些字段和成員變量。 – 2014-10-11 19:13:15

+0

@MobyDisk如果表單被關閉並打開,那麼隨機數仍然會每次都會重新生成。 – cybermonkey 2014-10-11 19:16:19

回答

2

當前標準的,將Random數字每天都在guessButton_Click功能運行時間覆蓋

您聲明RandomguessButton_Click函數內部,即每次guess按鈕被點擊時間稱爲(這也是內存泄漏!)。 要解決,聲明它作爲一個全局變量,在命名空間:

編輯:下面的代碼編譯正確,和完美的作品。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApplication5 
{ 

    public partial class Form1 : Form 
    { 
     int number = new Random().Next(1, 100); 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void guessButton_Click(object sender, EventArgs e) 
     { 

      int userGuess; 
      userGuess = int.Parse(guessText.Text); 
      label2.Text = "" + number; 


      if (userGuess > number) 
      { 
       resultLabel.Text = "Your guess is too high"; 
      } 

      else if (userGuess < number) 
      { 
       resultLabel.Text = "Your guess is too low."; 
      } 

      else if (userGuess == number) 
      { 
       resultLabel.Text = "That is correct!"; 
      } 

      guessText.Clear(); 



     } 

     private void exitButton_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 
    } 
} 
+1

謝謝@cybermonkey,這工作很好。我會upvote你,但這是我在網站上的第一天,它不會讓我呢。雖然我確實點擊了複選標記。 – 2014-10-11 19:45:26

+1

+1。沒有降低你的答案(至少現在)。經過一定數量的代表(我認爲它是2,000)後,SO會給你提供上漲和下降的分解。現在這個答案是在+2/-0。 – 2014-10-11 20:25:02

+0

我不認爲這是內存泄漏,因爲內存將被GC回收。 – Lukazoid 2017-10-07 12:22:24

0

你得到一個新的隨機數的每個按鈕單擊嘗試以下操作:

public partial class Form1 : Form 
{ 

    Random rand; 
    int number; 

    public Form1() 
    { 
     InitializeComponent(); 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     rand = new Random(); 
     number = rand.Next(1, 100); 
    } 

    private void guessButton_Click(object sender, EventArgs e) 
    { 
     int userGuess; 
     userGuess = int.Parse(guessText.Text); 

     label2.Text = "" + number; 


     if (userGuess > number) 
     { 
      resultLabel.Text = "Your guess is too high"; 
     } 

     else if (userGuess < number) 
     { 
      resultLabel.Text = "Your guess is too low."; 
     } 

     else if (userGuess == number) 
     { 
      resultLabel.Text = "That is correct!"; 
     } 

     guessText.Clear(); 
    } 


} 
+0

這解決了它總是改變數字的問題,但現在它似乎總是設置爲0.我嘗試初始化數字到公共類中的rand.Next(1,100),但它不允許這樣做。 – 2014-10-11 19:26:35

+0

此代碼有一個錯誤.. – cybermonkey 2014-10-11 19:43:03

+0

它適用於我。什麼是錯誤?這個例子作爲一個更清晰的例子,因爲工作是在構造函數而不是聲明中完成的。 – 2014-10-11 19:56:16