2014-10-11 118 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