我目前正在做一个关于拼写的小测验。 该程序显示一个问题和 4 个可能的答案。 现在我遇到的问题是我不知道如何更改正在使用的列表。 我有 2 个问题列表:StamQuestions 和 StateQuestions。
public void GiveAnswerA(object sender, EventArgs e)
{
if (TextAnswerA.Text == StamQuestions[numberQuestion].CorrectAnswer)
{
SetUI();
score++;
}
}
private void GiveAnswerB(object sender, EventArgs e)
{
if (TextAnswerB.Text == StamQuestions[numberQuestion].CorrectAnswer)
{
SetUI();
score++;
}
}
private void GiveAnswerC(object sender, EventArgs e)
{
if (TextAnswerC.Text == StamQuestions[numberQuestion].CorrectAnswer)
{
SetUI();
score++;
}
}
private void GiveAnswerD(object sender, EventArgs e)
{
if (TextAnswerD.Text == StamQuestions[numberQuestion].CorrectAnswer)
{
SetUI();
score++;
}
}
private void SetUI()
{
numberQuestion = rnd.Next(0, StamQuestions.Count);
CurrentQuestion.Text = StamQuestions[numberQuestion].QuestionText;
TextAnswerA.Text = StamQuestions[numberQuestion].Answers[0];
TextAnswerB.Text = StamQuestions[numberQuestion].Answers[1];
TextAnswerC.Text = StamQuestions[numberQuestion].Answers[2];
TextAnswerD.Text = StamQuestions[numberQuestion].Answers[3];
ResultAnswer.Text = "Punten : "+score.ToString();
}
现在我想要发生的是,当我按下按钮时,StamQuestions 被 StateQuestions 取代。
如有任何帮助,我们将不胜感激!
编辑:这是列表的定义
List<Question> StamQuestions = new List<Question>();
List<Question> StateQuestion = new List<Question>();
最佳答案
一般来说,您只需存储对当前使用的问题列表的引用,并根据需要交换该引用。
为currentQuestions
添加另一个类级变量:
private List<Question> stamQuestions = new List<Question>();
private List<Question> stateQuestions = new List<Question>();
private List<Question> currentQuestions = stamQuestions;
在任何地方使用currentQuestions
,例如:
public void GiveAnswerA(object sender, EventArgs e)
{
if (TextAnswerA.Text == currentQuestions[numberQuestion].CorrectAnswer)
{
SetUI();
score++;
}
}
然后根据需要交换到另一组,我怀疑在 SetUI
中:
private void SetUI()
{
currentQuestions = stateQuestions;
numberQuestion = rnd.Next(0, currentQuestions.Count);
CurrentQuestion.Text = currentQuestions[numberQuestion].QuestionText;
TextAnswerA.Text = currentQuestions[numberQuestion].Answers[0];
TextAnswerB.Text = currentQuestions[numberQuestion].Answers[1];
TextAnswerC.Text = currentQuestions[numberQuestion].Answers[2];
TextAnswerD.Text = currentQuestions[numberQuestion].Answers[3];
ResultAnswer.Text = "Punten : "+score.ToString();
}
或者您可能打算在列表之间翻转/翻转,在这种情况下您也可以这样做:
currentQuestions = (currentQuestions == stamQuestions) ? stateQuestions : stamQuestions;
关于c# - 如何在按下按钮时使用不同的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610275/