C# 如何保持跑步成绩?
我正在做一个小测试,我不知道如何在用户提交测试后更新当前分数。根据问题从错误到正确的顺序,分数可以波动25分,反之亦然C# 如何保持跑步成绩?,c#,C#,我正在做一个小测试,我不知道如何在用户提交测试后更新当前分数。根据问题从错误到正确的顺序,分数可以波动25分,反之亦然 public partial class _Default : System.Web.UI.Page { private int totalScore = 0; public void IncrementScore() { totalScore += 25; } protected void Page_Load(object sender, EventArgs e)
public partial class _Default : System.Web.UI.Page
{
private int totalScore = 0;
public void IncrementScore()
{
totalScore += 25;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblHeader.Text = "quiz not taken";
}
else
{
lblHeader.Text = "Score: " + totalScore;
}
}
protected void Submit_Click(object sender, EventArgs e)
{
/***************************************************************************/
if (IsValid)
if (txtAnswer.Text.Equals("primary", StringComparison.InvariantCultureIgnoreCase))
{
lblQuestionResult1.ForeColor = System.Drawing.Color.Green;
lblQuestionResult1.Text = "Correct";
}
else
{
lblQuestionResult1.ForeColor = System.Drawing.Color.Red;
lblQuestionResult1.Text = "Incorrect";
}
/***************************************************************************/
if (ddList.SelectedItem.Text.Equals("M:N"))
{
lblQuestionResult2.ForeColor = System.Drawing.Color.Green;
lblQuestionResult2.Text = "Correct";
}
else
{
lblQuestionResult2.ForeColor = System.Drawing.Color.Red;
lblQuestionResult2.Text = "Incorrect";
}
/***************************************************************************/
if (RadioButton4.Checked == true)
{
lblQuestionResult3.ForeColor = System.Drawing.Color.Green;
lblQuestionResult3.Text = "Correct";
}
else
{
lblQuestionResult3.ForeColor = System.Drawing.Color.Red;
lblQuestionResult3.Text = "Incorrect";
}
/***************************************************************************/
lblQuestionResult4.ForeColor = System.Drawing.Color.Red;
lblQuestionResult4.Text = "Incorrect";
if (Answer2.Checked && Answer3.Checked && !Answer1.Checked && !Answer4.Checked)
{
lblQuestionResult4.ForeColor = System.Drawing.Color.Green;
lblQuestionResult4.Text = "Correct";
}
}
}
递增法
private int totalScore = 0;
将不起作用,因为您会为每个HTTP请求获得一个新的\u Default
实例
您可以在课程中保持跑步成绩
但是,我建议在需要时,通过循环查看答案和与每个答案相关联的分数来重新计算总分。如果人们返回并更改答案(假设允许这样做),这将简化逻辑。将其修改为类似“查看、检查语法”的内容,而不是使用VS
受保护的无效页面加载(对象发送方、事件参数e)
{
}
//增量法
if(Session["TotalScore"]!=null)
{
totalScore += 25;
}
else
{
totalScore=int.Parse(Session["TotalScore"])+25;
}
public void IncrementScore(){//second part here}如果您想让它更简单,只需在原始问题中将变量总分设置为static即可
if(Session["TotalScore"]!=null)
{
totalScore += 25;
}
else
{
totalScore=int.Parse(Session["TotalScore"])+25;
}