随机数生成器问题

时间:2016-02-15 07:56:38

标签: c# random int tryparse

首先,我想说我还是C#的初学者,所以在向我解释信息时,请不要使用我不理解的复杂术语。 其次,我完成了大部分工作,我不是要求其他人完成我的工作,我只是在寻求帮助,因为我不明白什么是错的/为什么它不起作用。 第三,我的程序是不完整的,除非我的随机生成器工作,否则我无法完成它。 所以说到这一点,我遇到的问题是当我尝试运行该程序时,系统强调了“随机”这个词。在我的代码的开头,并说

  

"字段初始值设定项不能引用非静态字段,方法或   属性&#34 ;.

为什么这样做?如果我把两行代码放在" Public Guess()"部分然后编译器运行正常,它然后说我的" if"声明不会工作,因为容器" random"不存在。我不知道我还能做些什么,我真的非常感谢一些帮助。     我的代码如下:

public partial class Guess : Form
{
    /*This is a "Guess the number" program. Then this program is run,
     * I want to create two containers for the "TryParse" portion of this program
     and then I want a number to be randomly generated for the user to guess, then
     I want one last container to count how many guess it took the user.*/

    string number;
    int guess;
    Random random;
    int randomnumber;
    int counter;


    public Guess()
    {
        /*Once the program is initalized, I want the 2nd button hidden until the first one
         is clicked with a value in the textbox*/
        InitializeComponent();
        btnexe2.Hide();
        random = new Random();
        randomnumber = random.Next(0, 101);

    }
    private void btnClose_Click(object sender, EventArgs e)
    {
        //This closes the program//
        Close();
    }
    private void btnexe1_Click(object sender, EventArgs e)
    {
        /*This is where I will be doing most of my variable checking. First, 
         I want to check if the user left the textbox empty, if it is then 
         display a message box saying to enter a number.*/
        if (string.IsNullOrEmpty(tbnumber.Text))
        {
            MessageBox.Show("Please enter a number from 0-100.");
        }
        else
        {/*If it is not empty, then I want the system to determine if the variable 
         that has been entered can be converted to a int.*/
            number = Convert.ToString(tbnumber.Text);
            if (Int32.TryParse(number, out guess))
            {
                /*If the value can be converted, then i want the system to see if
         it is lower, higher, or equal to the random value. Then I want the fist button hidden,
         and the second one shown. Then I want to record how many times the user guessed.*/
                if (guess < randomnumber)
                {
                    btnexe1.Hide();
                    btnexe2.Show();
                    this.BackColor = System.Drawing.Color.LightSeaGreen;
                    lbloutput.Text = "Too Low";
                    counter=counter + 1;
                }
                else if (guess > randomnumber)
                {
                    btnexe1.Hide();
                    btnexe2.Show();
                    this.BackColor = System.Drawing.Color.SlateBlue;
                    lbloutput.Text = "Too High";
                    counter = counter + 1;
                }
                else
                {
                    lbloutput.Text = "Good Guess";
                    counter = counter + 1;
                }
            }
            else
            {
               /*If the value cannot be converted to a int, then display a message box saying so.*/
                MessageBox.Show("This is not a number. Please enter a number between 0-100.");                    
            }
        }
    }
    private void btnexe2_Click(object sender, EventArgs e)
    {/*I want to check if the user left the textbox empty, if it is then 
         display a message box saying to enter a number.*/
        if (string.IsNullOrEmpty(tbnumber.Text))
        {
            MessageBox.Show("Please enter a number from 0-100.");
        }
        else
        {/*If it is not empty, then I want the system to determine if the variable 
         that has been entered can be converted to a int.*/
            number = Convert.ToString(tbnumber.Text);
            if (Int32.TryParse(number, out guess))
            {
                /*If the value can be converted, then I want the system to see if
        it is lower, higher, or equal to the random value. Then I want to record how
                 many times the user guessed.*/
                if (guess < randomnumber)
                {
                    lbloutput.Text = "Too Low";
                    this.BackColor = System.Drawing.Color.LightSeaGreen;
                    counter = counter + 1;
                }
                else if (guess > randomnumber)
                {
                    lbloutput.Text = "Too High";
                    this.BackColor = System.Drawing.Color.SlateBlue;
                    counter = counter + 1;
                }
                else
                {
                    lbloutput.Text = "Good Guess";
                    counter = counter + 1;
                    lblcounter.Text = "You guessed " + counter + " times.";
                }
            }
            else
            {
                /*If the value cannot be converted to a int, then display a message box saying so.*/
                MessageBox.Show("This is not a number. Please enter a number between 0-100");

            }
        }
    }
}

4 个答案:

答案 0 :(得分:3)

像这样更改您的代码。您试图在类中调用方法Next,这是不允许的,当您在构造函数中移动完整代码时,您的变量范围仅存在于该构造函数中,因此在另一个方法中访问的变量将不起作用。所以解决方案是在类级别定义变量,但在构造函数

中初始化它
  Random random; 
  int randomnumber;
    public Guess()
    {
        /*Once the program is initalized, I want the 2nd button hidden until the first one
         is clicked with a value in the textbox*/

        InitializeComponent();
        btnexe2.Hide();
        random = new Random();
        randomnumber = random.Next(0, 101);
    }

答案 1 :(得分:1)

您应该初始化方法内的变量(初始化意味着向变量添加值 - 使用“new”关键字);

尝试这样的事情:

class Guess {
    Random random;
    int randomNumber;

    public Guess() {
        random = new Random();
        randomnumber = random.Next(0, 101);
        //Rest of the code
    }
}

答案 2 :(得分:1)

我认为这个答案可以帮到你。您不能使用实例变量初始化另一个实例变量,因为您编写它们的顺序不是每个定义编译器创建它们的顺序。

A field initializer cannot reference the nonstatic field, method, or property

答案 3 :(得分:0)

尝试更改此

Random random = new Random();
int randomnumber = random.Next(0, 101);

public Guess()
{
    /*Once the program is initalized, I want the 2nd button hidden until the first one
     is clicked with a value in the textbox*/
    InitializeComponent();
    btnexe2.Hide();
}

到这个

 private Random _Random;
 private int _RandomNumbrer;
 public Guess()
 {

   _Random = new Random();
   _RandomNumbrer = random.Next(0, 101);

   /*Once the program is initalized, I want the 2nd button hidden until the first one
   is clicked with a value in the textbox*/
   InitializeComponent();
   btnexe2.Hide();
}

你几乎在那里尝试做什么