0

我正在做一个迷你测试,但我不确定如何在用户提交测试后更新当前分数的运行分数。根据问题是否从错误到正确,分数可能会波动 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)
{

    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";
    }
}
}
4

2 回答 2

2

递增的方法

private int totalScore = 0;

将不起作用,因为您会_Default为每个 HTTP 请求获取一个新实例。

您可以将您的跑步成绩保持在Session.

但是,我建议始终在需要时通过循环遍历与每个答案相关的答案和分数来重新计算总分。如果人们返回并更改答案(假设允许这样做),这会简化逻辑。

于 2013-03-05T18:15:04.650 回答
0

将其修改为查看,检查语法,未使用 VS

protected void Page_Load(object sender, EventArgs e) {

if (!IsPostBack)
{
    lblHeader.Text = "quiz not taken";
}
else
{
    Session["TotalScore"] = ""+totalScore; //Storing it in a session
    lblHeader.Text = "Score: " + Session["TotalScore"];
}

}

//递增方法

if(Session["TotalScore"]!=null)
 { 
  totalScore += 25;
 } 
else
{
totalScore=int.Parse(Session["TotalScore"])+25;
}
于 2013-03-05T18:53:58.317 回答