【问题标题】:Random number generator Issue随机数生成器问题
【发布时间】:2016-05-26 01:17:10
【问题描述】:

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

"字段初始值设定项不能引用非静态字段、方法或 属性”。

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

            }
        }
    }
}

【问题讨论】:

    标签: c# random int tryparse


    【解决方案1】:

    像这样更改您的代码。您正在尝试在不允许的类中调用 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);
        }
    

    【讨论】:

    • 当我这样做时,我的“if”语句变得一团糟,指出“在当前上下文中不存在“random”这个名称。我在发布的原始问题中说过这个。它的驱动我疯了!大声笑
    • 不,我猜你是在构造函数中移动整个代码,这意味着你在构造函数中定义变量,所以随机变量不会在另一种方法 btnexe2_click 中被识别 ....你能发布更新的答案吗?
    • 还要确保在 if 语句中使用 randomnumber 而不是 random
    • 另外你的 tryParse 逻辑是错误的...... TryParse 在成功解析时返回 true,所以在 if else 逻辑中交换你的代码
    • 我不太确定这次我做了什么让它工作,但现在它工作得很好。所以我拿出随机数生成器并设置一个静态值来测试其他所有内容。完成后,我删除了静态值并添加了随机生成器,然后添加了 viola。我不明白我做错了什么。这是纯粹的魔法吗?再次感谢您的帮助 Viru 我真的很感激,我也感谢其他所有人的帮助。
    【解决方案2】:

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

    试试这样的:

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

    【讨论】:

    • 当我按照你的方式做时,我的所有错误都消失了,我得到 1 个新错误代替所有其他错误。现在系统说“方法必须有返回类型”。我会把“return random”放在“public Guess()”方法的底部吗? (我认为这是一种方法)
    • @Spr89 Guess() 是一个构造方法。而且它没有返回类型。您的代码中已经有一个构造函数方法,在我的回答中,我的意思是您应该将这两个初始化行添加到您已经存在的构造函数中。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      尝试改变这个

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

      你已经完成了你想做的事

      【讨论】:

      • 当我这样做时,它与 Viru 的帖子相同,我的“if”语句变得混乱。当我使用你的下划线时,我最终会得到一个全新的错误世界,不太确定如果没有“int”,生成器将如何工作。
      猜你喜欢
      • 2017-04-10
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2018-01-03
      • 2013-10-21
      • 1970-01-01
      相关资源
      最近更新 更多