C# c语言中随机数猜测游戏的问题#
所以我正在为学校做一个项目,在一个猜谜游戏中,随机生成一个介于1-100之间的数字。目前我遇到的唯一问题是,每次用户输入新号码时,生成的号码也会发生变化。我试着将代码放入表单加载器中生成数字,但后来在程序中无法访问它。这是我到目前为止所拥有的C# c语言中随机数猜测游戏的问题#,c#,C#,所以我正在为学校做一个项目,在一个猜谜游戏中,随机生成一个介于1-100之间的数字。目前我遇到的唯一问题是,每次用户输入新号码时,生成的号码也会发生变化。我试着将代码放入表单加载器中生成数字,但后来在程序中无法访问它。这是我到目前为止所拥有的 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syst
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void guessButton_Click(object sender, EventArgs e)
{
int userGuess;
userGuess = int.Parse(guessText.Text);
Random rand = new Random();
int number = rand.Next(1, 100);
label2.Text = "" + number;
if (userGuess > number)
{
resultLabel.Text = "Your guess is too high";
}
else if (userGuess < number)
{
resultLabel.Text = "Your guess is too low.";
}
else if (userGuess == number)
{
resultLabel.Text = "That is correct!";
}
guessText.Clear();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Windows窗体应用程序5
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有无效猜测按钮\u单击(对象发送者,事件参数e)
{
int-userGuess;
userGuess=int.Parse(guessText.Text);
Random rand=新的Random();
整数=rand.Next(1100);
标签2.Text=”“+编号;
如果(用户猜测>数字)
{
resultLabel.Text=“您的猜测太高”;
}
else if(userGuess
目前的情况是,每次运行猜测按钮
功能时,都会覆盖随机数
您正在guess按钮\u Click
函数中声明Random
,该函数在每次单击guess
按钮时调用(这也是内存泄漏!)。
若要修复,请在命名空间中将其声明为全局变量:
编辑:下面的代码编译正确,工作完美
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
int number = new Random().Next(1, 100);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void guessButton_Click(object sender, EventArgs e)
{
int userGuess;
userGuess = int.Parse(guessText.Text);
label2.Text = "" + number;
if (userGuess > number)
{
resultLabel.Text = "Your guess is too high";
}
else if (userGuess < number)
{
resultLabel.Text = "Your guess is too low.";
}
else if (userGuess == number)
{
resultLabel.Text = "That is correct!";
}
guessText.Clear();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Windows窗体应用程序5
{
公共部分类Form1:Form
{
int number=new Random().Next(1100);
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有无效猜测按钮\u单击(对象发送者,事件参数e)
{
int-userGuess;
userGuess=int.Parse(guessText.Text);
标签2.Text=”“+编号;
如果(用户猜测>数字)
{
resultLabel.Text=“您的猜测太高”;
}
else if(userGuess
每次单击按钮都会得到一个新的随机数。请尝试以下操作:
public partial class Form1 : Form
{
Random rand;
int number;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
rand = new Random();
number = rand.Next(1, 100);
}
private void guessButton_Click(object sender, EventArgs e)
{
int userGuess;
userGuess = int.Parse(guessText.Text);
label2.Text = "" + number;
if (userGuess > number)
{
resultLabel.Text = "Your guess is too high";
}
else if (userGuess < number)
{
resultLabel.Text = "Your guess is too low.";
}
else if (userGuess == number)
{
resultLabel.Text = "That is correct!";
}
guessText.Clear();
}
}
公共部分类表单1:表单
{
随机兰德;
整数;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
rand=新随机数();
编号=下一个兰特(1100);
}
私有无效猜测按钮\u单击(对象发送者,事件参数e)
{
int-userGuess;
userGuess=int.Parse(guessText.Text);
标签2.Text=”“+编号;
如果(用户猜测>数字)
{
resultLabel.Text=“您的猜测太高”;
}
else if(userGuess
表单加载器是放置表单的正确位置。查找哪些字段和成员变量。@MobyDisk如果窗体关闭并打开,它将不起作用,每次都会重新生成随机数。当我将其移动到表单加载程序并尝试编译时,它告诉我名称“number”在当前上下文中不存在。这是因为您正在将声明和赋值移动到Form1\u Load
方法中。在类级别上声明变量将使它可以从Form1\u Load
和guessButton\u Click
@Timkister这修复了它总是更改数字的问题,但现在它似乎总是设置为0。我试图在public类中将number初始化为rand.Next(1100),但它不允许这样做。这对我来说很好。什么是错误?这个例子是一个更清晰的例子,因为工作是在构造函数中完成的,而不是在声明中完成的。我自己制作了这个程序,但我没有遇到任何问题,因为每次都没有将数字设置为0me@Sybren在代码中,随机数始终设置为0,可能是因为编译器不知道它是一个数字。尝试运行此代码,每次都会得到一个新的随机数。谢谢@cybermonkey,它工作得非常好。我会投票给你,但这是我在网站上的第一天,它不会让我。我确实为你点击了复选标记。没有人对你的答案投反对票(至少现在是这样)。经过一定数量的代表(我想是2000),所以给你上下票数的细目。