即时通讯设法使测验计划,但我的字符串阿尔法保存只有最后一个问题(如果question1被回答错了,它保存了它的错误,但如果question2是错误的,它只保存了问题2是错误回答覆写问题1是错误的回答。我想保存了错误的回答所有的问题不仅是最后一个,任何人都知道我在哪里出错在我的代码?字符串显示只有最后一个对象我添加(C#)
public partial class Exam : System.Web.UI.Page
{
ExamQuestionList questions;
int questionNumber;
string alfa;
public int Score
{
get { return (int)ViewState["score"]; }
}
protected void Page_Load(object sender, EventArgs e)
{
// initialise questionNumber and score
if (!IsPostBack)
{
ViewState["questionNumber"] = 0;
ViewState["score"] = 0;
}
questions = (ExamQuestionList)Application["questions"];
// update questionNumber
questionNumber = (int)ViewState["questionNumber"] + 1;
ViewState["questionNumber"] = questionNumber;
}
protected void Page_PreRender(object sender, EventArgs e)
{
// display next question
Question q = questions[questionNumber - 1];
lblQuestion.Text = "Question " + questionNumber + ": " + q.QuestionText;
rblOptions.DataSource = q.Options;
rblOptions.DataBind();
rblOptions.SelectedIndex = 0;
}
protected void cmdNextQuestion_Click(object sender, EventArgs e)
{
// update score based on the answer to the previous question
int previousAnswer = rblOptions.SelectedIndex;
Question previousQuestion = questions[questionNumber - 2];
if (previousAnswer == previousQuestion.CorrectAnswer)
ViewState["score"] = (int)ViewState["score"] + 1;
if (previousAnswer != previousQuestion.CorrectAnswer)
alfa =(questionNumber-1).ToString();
if (alfa == null)
Label1.Text ="";
if (alfa != null)
Label1.Text = "you gave wrong answer to"+alfa;
// redirect to Result.aspx, if this is the last question
if (questionNumber == questions.Length) cmdNextQuestion.PostBackUrl = "~/Result.aspx";
}
}
我在代码中没有看到任何类型的循环或列表。 – gunr2171