在我编程的数学游戏中,我的Windows窗体中有标签,其中数字显示总和。
当我点击button1
时,他们应该更改为其他数字,但他们不会。应更改的标签称为lblNumber1
和lblNumber2
。
这是我的代码:
public partial class plussommen : Form
{
int counter = 0;
int correct = 0;
int incorrect = 0;
Random rand = new Random();
int number1, number2;
int answer;
string sum;
public plussommen()
{
InitializeComponent();
number1 = rand.Next(1, 50);
number2 = rand.Next(1, 50);
sum = number1 + " + " + number2 + " = ";
answer = number1 + number2;
}
private void plussommen_Load(object sender, EventArgs e)
{
if (counter < 5)
{
lblNumber1.Text = number1.ToString();
lblNumber2.Text = number2.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
lblCorrect.Text = correct.ToString();
lblIncorrect.Text = incorrect.ToString();
if (textBox1.Text == answer.ToString())
{
MessageBox.Show("Answer correct!");
correct = correct + 1;
}
else
{
MessageBox.Show("Answer incorrect!");
incorrect = incorrect + 1;
}
if (counter < 5)
{
lblNumber1.Text = number1.ToString();
lblNumber2.Text = number2.ToString();
}
else
{
if (counter == 5)
{
MessageBox.Show("You've made five sums, choose another kind of sum.");
this.Close();
Form1 menu = new Form1();
menu.Show();
}
}
}
}
答案 0 :(得分:0)
要在每次点击时更改标签上的值,您需要每次都生成新的随机数。
if (counter < 5)
{
number1 = rand.Next(1, 50);
number2 = rand.Next(1, 50);
lblNumber1.Text = number1.ToString();
lblNumber2.Text = number2.ToString();
}
答案 1 :(得分:0)
在您的代码中,您不会在初始化后随时更改number1
和number2
的值。这就是为什么他们在每次点击按钮后都没有更新。
您需要做的是在每次点击后更新数字和答案:
private void button1_Click(object sender, EventArgs e)
{
//...
if (counter < 5)
{
number1 = rand.Next(1, 50);
number2 = rand.Next(1, 50);
answer = number1 + number2;
lblNumber1.Text = number1.ToString();
lblNumber2.Text = number2.ToString();
}
//...
}