c#范围问题,简单修复我敢肯定

时间:2011-10-09 20:22:31

标签: c# scoping

所以我有以下代码,它几乎完美无缺,除了无论我做什么我不能得到像这样的部分的事实:for (int.parse(txtGuess.Text) == numbGen)它无法识别'numbGen'无论在哪里我把它放在代码中。我不能将它放在按钮点击功能中,因为我不想更改数字,除非他们已经正确或重新打开表格。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;


    namespace WindowsApplication1
    {
        public partial class rndNum : Form
        {
            public rndNum()
            {
            }

            private void rndNum_Load(object sender, EventArgs e)
            {
                int numbGen = RandMake(0, 100);
            }

            private void txtGuess_TextChanged(object sender, EventArgs e)
            {

            }

            private void btnEval_Click(object sender, EventArgs e)
            {
                int guesses = 0;
                while (txtGuess.Text != "")
                {
                    if (int.Parse(txtGuess.Text) == numbGen)
                    {
                        MessageBox.Show("You got it!", "Congratulations!");
                        break;
                    }
                    else if (int.Parse(txtGuess.Text) > numbGen)
                    {
                        MessageBox.Show("Sorry, but you're too high. The number was " + numbGen + "!", "Please try again.");
                        txtGuess.Clear();
                        txtGuess.Focus();
                        guesses++;
                        break;
                    }
                    else if (int.Parse(txtGuess.Text) < numbGen)
                    {
                        MessageBox.Show("Sorry, but you're too low. The number was " + numbGen + "!", "Please try again.");
                        txtGuess.Clear();
                        txtGuess.Focus();
                        guesses++;
                        break;
                    }
                }
            }



            private static int RandMake(int min, int max)
            {
                Random mkRnd = new Random();
                return mkRnd.Next(min, max);
            }
}

}

4 个答案:

答案 0 :(得分:3)

numbGen必须是班级成员。

更改

        private void rndNum_Load(object sender, EventArgs e)
        {
            int numbGen = RandMake(0, 100);
        }

        private int numbGen;
        private void rndNum_Load(object sender, EventArgs e)
        {
            numbGen = RandMake(0, 100);
        }

实际上你不需要在Form.Load中进行初始化,你可以直接初始化一个类成员。

    public partial class rndNum : Form
    {
        private int numbGen = RandMake(0, 100);
        public rndNum()
        {
        }

并进一步细化:如果您想确保不更改值,可以将其设为readonly

    public partial class rndNum : Form
    {
        private readonly int numbGen = RandMake(0, 100);
        public rndNum()
        {
        }

答案 1 :(得分:2)

int numbGen;
private void rndNum_Load(object sender, EventArgs e)
{
    numbGen = RandMake(0, 100);
}

试试这个!!!

答案 2 :(得分:2)

其他人已经对您注意到的范围问题发表了评论。但是你的RandMake方法也存在缺陷。您不应为每个数字创建Random的实例,而是重用该实例。

这里的问题是new Random()使用时间作为种子,时间仅每隔几毫秒改变一次。这意味着如果您在该时间间隔内多次拨打RandMake,您将获得相同的“随机”号码。

这似乎不是一个直接的问题因为你只调用一次,但你将来应该知道这一点。

答案 3 :(得分:0)

当您在方法内部放置numbGen声明时,该数字仅存在于该函数范围内。你必须把它放在外面这样:

        int numbGen;
        private void rndNum_Load(object sender, EventArgs e)
        {
            numbGen = RandMake(0, 100);
        }

int numbGen; private void rndNum_Load(object sender, EventArgs e) { numbGen = RandMake(0, 100); }