C# 随机数发生器问题

C# 随机数发生器问题,c#,random,int,tryparse,C#,Random,Int,Tryparse,首先,我想说,我仍然是C#的初学者,所以在向我解释信息时,请不要使用我无法理解的复杂术语。 第二,我已经完成了大部分工作,我没有要求其他人完成我的工作,我只是请求帮助,因为我不明白哪里出了问题/为什么不起作用。 第三,我的程序不完整,除非我的随机发生器工作,否则我无法完成它。 这么说来,我遇到的问题是,当我试图运行程序时,系统在代码开头加上“random”一词,然后说 字段初始值设定项不能引用非静态字段、方法或 财产” 为什么要这样做?如果我把两行代码放在“Public Guess()”部分中,

首先,我想说,我仍然是C#的初学者,所以在向我解释信息时,请不要使用我无法理解的复杂术语。 第二,我已经完成了大部分工作,我没有要求其他人完成我的工作,我只是请求帮助,因为我不明白哪里出了问题/为什么不起作用。 第三,我的程序不完整,除非我的随机发生器工作,否则我无法完成它。 这么说来,我遇到的问题是,当我试图运行程序时,系统在代码开头加上“random”一词,然后说

字段初始值设定项不能引用非静态字段、方法或 财产”

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

            }
        }
    }
}
公共部分类猜测:表单
{
/*这是一个“猜数字”程序。然后这个程序运行,
*我想为这个程序的“TryParse”部分创建两个容器
然后我希望随机生成一个数字,供用户猜测,然后
我想要最后一个容器来计算用户猜了多少次*/
字符串编号;
智力猜测;
随机;
整数随机数;
整数计数器;
公众猜测
{
/*一旦程序初始化,我希望第二个按钮隐藏到第一个按钮
使用文本框中的值单击*/
初始化组件();
btnexe2.Hide();
随机=新随机();
randomnumber=random.Next(0,101);
}
私有void btnClose_单击(对象发送者,事件参数e)
{
//这将关闭程序//
Close();
}
私有void btnexe1\u单击(对象发送方,事件参数e)
{
/*我将在这里进行大部分变量检查。首先,
我想检查用户是否将文本框留空,如果是的话
显示一个消息框,提示输入一个数字*/
if(string.IsNullOrEmpty(tbnumber.Text))
{
MessageBox.Show(“请输入0-100之间的数字”);
}
其他的
{/*如果它不是空的,那么我希望系统确定变量
已输入的可转换为整数*/
number=Convert.ToString(tbnumber.Text);
if(Int32.TryParse(数字,超出猜测))
{
/*如果该值可以转换,那么我希望系统查看
它是更低、更高或等于随机值。然后我想隐藏First按钮,
然后我想记录用户猜了多少次*/
如果(猜测<随机数)
{
btnexe1.Hide();
btnexe2.Show();
this.BackColor=System.Drawing.Color.LightSeaGreen;
lbloutput.Text=“过低”;
计数器=计数器+1;
}
else if(猜测>随机数)
{
btnexe1.Hide();
btnexe2.Show();
this.BackColor=System.Drawing.Color.SlateBlue;
lbloutput.Text=“过高”;
计数器=计数器+1;
}
其他的
{
lbloutput.Text=“猜得好”;
计数器=计数器+1;
}
}
其他的
{
/*如果该值无法转换为int,则显示一个消息框,说明此情况*/
MessageBox.Show(“这不是一个数字,请输入一个介于0-100之间的数字”);
}
}
}
私有void btnexe2\u单击(对象发送方,事件参数e)
{/*我想检查用户是否将文本框留空,如果是的话
显示一个消息框,提示输入一个数字*/
if(string.IsNullOrEmpty(tbnumber.Text))
{
MessageBox.Show(“请输入0-100之间的数字”);
}
其他的
{/*如果它不是空的,那么我希望系统确定变量
已输入的可转换为整数*/
number=Convert.ToString(tbnumber.Text);
if(Int32.TryParse(数字,超出猜测))
{
/*如果该值可以转换,那么我希望系统查看
它是更低、更高或等于随机值。然后我想记录下
很多次用户都猜到了*/
如果(猜测<随机数)
{
lbloutput.Text=“过低”;
this.BackColor=System.Drawing.Color.LightSeaGreen;
计数器=计数器+1;
}
else if(猜测>随机数)
{
lbloutput.Text=“过高”;
this.BackColor=System.Drawing.Color.SlateBlue;
计数器=计数器+1;
}
其他的
{
lbloutput.Text=“猜得好”;
计数器=计数器+1;
lblcounter.Text=“您猜到了”+计数器+“次。”;
}
}
其他的
{
/*如果该值无法转换为int,则显示一个消息框,说明此情况*/
MessageBox.Show(“这不是一个数字,请输入一个介于0-100之间的数字”);
}
}
}
}

像这样更改代码。您试图调用类中的下一个方法,这是不允许的,当您在构造函数中移动完整的代码时,您的变量范围仅存在于构造函数中
  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();
}
class Guess {
    Random random;
    int randomNumber;

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