C# 范围问题,我确定可以简单修复
所以我有以下代码,它的工作几乎完美无缺,除了以下事实:无论我做什么我都无法得到类似的部分: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
numbGen
必须是类成员。改为
实际上
不需要将初始化放在
Form.Load
中,可以直接初始化类成员。并进一步细化:如果您想确保值没有更改,您可以将其设置为只读
numbGen
must be a class member.Change
to
actually you don't need to put the initialization in
Form.Load
, you can initialize a class member directly.and to further refine: if you want to make sure the values is not changed you can make it
readonly
试试这个!
try this!!!
其他人已经对您注意到的范围问题发表了评论。但你的 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 ofRandom
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 callRandMake
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.
当您将 numbGen 的声明放在方法内部时,该数字仅存在于该函数范围内。你必须像这样把它放在外面:
<代码>
<代码>
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: