2013-03-05 51 views
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"; 
    } 
} 
} 

回答

0

它修改成類似看,語法檢查,不使用VS

保護無效的Page_Load(對象發件人,EventArgs的){

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; 
} 
+0

代碼的第二部分在哪裏去? – trama 2013-03-05 19:24:49

+0

public void IncrementScore() { //第二部分在這裏 } – din 2013-03-05 19:28:20

+0

如果您想讓它更簡單,只需在原始問題中將可變總分數設置爲靜態 – din 2013-03-05 23:51:33

2

遞增

private int totalScore = 0; 

因爲你得到的_Default一個新實例每個HTTP請求將無法正常工作的方法。

您可以在Session中保持運行得分。

但是,我會建議總是在需要時重新計算總分,並根據需要循環查看與每個答案相關的答案和分數。如果人們回過頭來改變答案(假設允許的話),這簡化了邏輯。