我在下面有这个数学游戏,如果用户给出正确的答案,分数应该增加 5。但是,它总是卡在 5 并且永远不会增加。我想知道为什么?我声明了 !IsPostBack 变量,因此它停止重置 int,除非在页面刷新时,建议赞赏。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
int score;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
score = 0;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int sayi1;
int sayi2;
Random rnd = new Random();
sayi1 = rnd.Next(0, 100);
sayi2 = rnd.Next(0, 100);
Label1.Text = sayi1.ToString();
Label3.Text = sayi2.ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
int entry = Convert.ToInt32(TextBox1.Text);
int number1 = Convert.ToInt32(Label1.Text);
int number2 = Convert.ToInt32(Label3.Text);
int total = number1 + number2;
if (entry == total)
{
score += 5;
Label5.Text = score.ToString();
}
else
{
score -= 2;
Label5.Text = score.ToString();
}
}
}