C# 如何在C语言的整个代码中加入一个随机生成器
我目前正在做一个C窗口形式的数学游戏。我必须生成2个随机数1-99,并为每个数学函数显示它们。我有两个按钮,一个启动游戏,一个提交答案。我的错误是我做了两组不同的随机数,使得“开始”按钮与“提交”按钮的随机数不同。我希望每个部分都有相同的随机数,我该怎么做?我有一个想法,那就是把它放在公共课form1中,但它一直说我有一个错误C# 如何在C语言的整个代码中加入一个随机生成器,c#,visual-studio,C#,Visual Studio,我目前正在做一个C窗口形式的数学游戏。我必须生成2个随机数1-99,并为每个数学函数显示它们。我有两个按钮,一个启动游戏,一个提交答案。我的错误是我做了两组不同的随机数,使得“开始”按钮与“提交”按钮的随机数不同。我希望每个部分都有相同的随机数,我该怎么做?我有一个想法,那就是把它放在公共课form1中,但它一直说我有一个错误 using System; using System.Collections.Generic; using System.ComponentModel; using Sy
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 MathLearning
{
public partial class Form1 : Form
{
//This is where I believe it is supposed to go
//Random ranNum = new Random();
//int num1 = ranNum.Next(1, 100);
//int num2 = ranNum.Next(1, 100);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Text = "Next";
if (rdoAddition.Checked)
{
int answer = num1 + num2;
lblQuestion.Text = $"{num1 + " + " + num2 + "?"}";
}
if (rdoSubtraction.Checked)
{
int answer = num1 / num2;
lblQuestion.Text = $"{num1 + " / " + num2 + "?"}";
}
if (rdoMultiplication.Checked)
{
int answer = num1 * num2;
lblQuestion.Text = $"{num1 + " * " + num2 + "?"}";
}
if (rdoDivison.Checked)
{
int answer = num1 / num2;
lblQuestion.Text = $"{num1 + " / " + num2 + "?"}";
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
Random ranNum = new Random();
int num1 = ranNum.Next(1, 100);
int num2 = ranNum.Next(1, 100);
int i = 0;
if(rdoGenderM.Checked)
{
if (rdoAddition.Checked)
{
int answer = num1 + num2;
if(int.Parse(txtAnswer.Text) == answer)
{
lblQuestion.Text = $"{"Good Job Sir\nYou got " + i + " correct Out of " + i + " problems."}";
}
else
lblQuestion.Text = $"{num1 + " + " + num2 + "?\nThe answer is " + answer}";
}
}
//else if(rdoGenderF.Checked)
//{
// lblQuestion.Text = $"{"Good Job Madam\nYou got " + i + " correct Out of " + i + " problems."}";
//}
//else
// lblQuestion.Text = $"{"Good Job Unknown gender\nYou got " + i + " correct Out of " + i + " problems."}";
}
}
}
您走的是正确的道路,只需在构造函数中初始化它们,或者在任何有意义的地方初始化它们
private Random ranNum = new Random();
private int num1;
private int num2;
public Form1()
{
InitializeComponent();
num1 = ranNum.Next(1, 100);
num2 = ranNum.Next(1, 100);
}
长话短说
字段/属性初始值设定项无法访问非静态成员(如随机场),如果是这种情况,它将引入依赖顺序的初始化逻辑,该逻辑很容易被破坏,并且依赖于实现。但是,您可以访问构造函数中的实例成员。调用构造函数时,已调用所有其他字段和属性初始值设定项,这样可以安全地调用实例成员。这应该可以工作,每次单击“开始”时,它都会生成新的数字
using System.Collections.Generic;
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MathLearning
{
public partial class Form1 : Form
{
//This is where I believe it is supposed to go
Random ranNum = new Random();
int num1 = 0;
int num2 = 0;
int i = 0;
int numProblems = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Text = "Next";
int num1 = ranNum.Next(1, 100);
int num2 = ranNum.Next(1, 100);
if (rdoAddition.Checked)
{
int answer = num1 + num2;
lblQuestion.Text = $"{num1 + " + " + num2 + "?"}";
}
if (rdoSubtraction.Checked)
{
int answer = num1 / num2;
lblQuestion.Text = $"{num1 + " / " + num2 + "?"}";
}
if (rdoMultiplication.Checked)
{
int answer = num1 * num2;
lblQuestion.Text = $"{num1 + " * " + num2 + "?"}";
}
if (rdoDivison.Checked)
{
int answer = num1 / num2;
lblQuestion.Text = $"{num1 + " / " + num2 + "?"}";
}
numProblems++;
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if(rdoGenderM.Checked)
{
if (rdoAddition.Checked)
{
int answer = num1 + num2;
if(int.Parse(txtAnswer.Text) == answer)
{
i++;
lblQuestion.Text = $"{"Good Job Sir\nYou got " + i + " correct Out of " + numProblems + " problems."}";
}
else
lblQuestion.Text = $"{num1 + " + " + num2 + "?\nThe answer is " + answer}";
}
}
//else if(rdoGenderF.Checked)
//{
// lblQuestion.Text = $"{"Good Job Madam\nYou got " + i + " correct Out of " + numProblems + " problems."}";
//}
//else
// lblQuestion.Text = $"{"Good Job Unknown gender\nYou got " + i + " correct Out of " + numProblems + " problems."}";
}
}
}
你相信得对。将代码从btnSubmit_中删除,单击并将其放在您认为合适的位置。当您执行9/2时,您还应该使用十进制而不是int,因为int的答案是4,而不是4.5。您需要十进制或双精度来获取十进制数。OP在btnStart_Click方法中声明int-answer。你也这么做了。但是它不起作用——如果你想展示所有的代码,你应该确保它能工作。