随机数生成器问题

Random number generator Issue

首先我想说我还是一个C#的初学者,所以在向我解释信息时,请不要使用我不会理解的复杂行话。 其次,我已经完成了大部分工作,我没有要求别人完成我的工作,我只是寻求帮助,因为我不明白什么是 wrong/why 它不起作用。 第三,我的程序不完整,除非我的随机生成器工作,否则我无法完成它。 因此,话虽如此,我遇到的问题是当我尝试 运行 程序时,系统在我的代码开头加下划线 "random" 并说

"A field initializer cannot reference the non-static field, method, or property".

为什么要这样做?如果我将这两行代码放在“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");

            }
        }
    }
}

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

中初始化它
  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);
    }

尝试改变这个

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();
}

你已经快完成你想要做的事情了

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

尝试这样的事情:

class Guess {
    Random random;
    int randomNumber;

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

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

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