简体   繁体   English

如何保持得分?

[英]How to keep a running score?

I am making a mini test and I am not sure how to go about making a running score that will update the current score after the user submits the test. 我正在做一个迷你测试,我不知道如何在用户提交测试后制作一个能够更新当前分数的跑分数。 The score can fluctuate by 25 points depending on if the question goes from wrong to right, and vice versa. 分数可以波动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";
    }
}
}

The approach of incrementing 递增的方法

private int totalScore = 0;

will not work because you get a new instance of _Default for every HTTP request. 将无法正常工作,因为您为每个HTTP请求获取了一个新的_Default实例。

You can keep your running score in Session . 您可以在Session保持您的运行得分。

However, I would instead suggest always recalculating the total score when needed by looping through the answers and score associated with each answer as needed. 但是,我建议总是在需要时通过循环搜索答案并根据需要与每个答案相关联的分数来重新计算总分。 This simplifies the logic if people go back and change an answer (assuming it is permitted to do that). 如果人们回去并改变答案(假设允许这样做),这简化了逻辑。

Modify it to something like see, check syntax, was not using VS 修改它看起来像看,检查语法,没有使用VS.

protected void Page_Load(object sender, EventArgs e) { 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"];
}

} }

//increment method //增量方法

if(Session["TotalScore"]!=null)
 { 
  totalScore += 25;
 } 
else
{
totalScore=int.Parse(Session["TotalScore"])+25;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM