C# 范围问题,我确定可以简单修复

发布于 2024-12-09 02:29:44 字数 2204 浏览 1 评论 0原文

所以我有以下代码,它的工作几乎完美无缺,除了以下事实:无论我做什么我都无法得到类似的部分: for (int.parse(txtGuess.Text) == numbGen)< /code> 无论我将“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);
            }
}

}

So I have the following code, and It works nearly flawlessly, except for the fact that no matter what I do I cannot get the parts that are like: for (int.parse(txtGuess.Text) == numbGen) it will not recognize 'numbGen' no matter where I place it in the code. I cannot place it inside the button click function because I don't want the number to change, unless they've gotten it correct or re-opened the form.

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只想待在家 2024-12-16 02:29:44

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

并进一步细化:如果您想确保值没有更改,您可以将其设置为只读

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

numbGen must be a class member.

Change

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

to

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

actually you don't need to put the initialization in Form.Load, you can initialize a class member directly.

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

and to further refine: if you want to make sure the values is not changed you can make it readonly

    public partial class rndNum : Form
    {
        private readonly int numbGen = RandMake(0, 100);
        public rndNum()
        {
        }
风柔一江水 2024-12-16 02:29:44
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);
}

try this!!!

栖迟 2024-12-16 02:29:44

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

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

这似乎不是一个迫在眉睫的问题,因为您只调用它一次,但您将来应该意识到这一点。

The others already commented on the scoping issue you noticed. But your RandMake method is flawed too. You should not create an instance of Random for each number, but reuse the instance.

The problem here is that new Random() uses the time as seed, and the time only changes every few milliseconds. This means that if you call RandMake several times within that time interval you will get the same "random" number.

This doesn't seem to be a immediate problem because you only call it once, but you should be aware of this in the future.

别把无礼当个性 2024-12-16 02:29:44

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

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

<代码>

When you put numbGen's declaration on the inside of a method, the number only exist in that functions scope. You have to place it outside like this:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文