【发布时间】:2012-02-13 10:02:20
【问题描述】:
我创建了一个测验应用程序,并使用 switch 语句来解决问题。即从 1 到 2 到 3 到 4 等等。 但是,一旦用户完成了一次测验并想重新开始,他们将不得不重新完成所有相同的问题。 (设计是如果他们弄错了,应用程序就会退出)。 因此,我想看看是否有一种方法可以随机化 QuestionCount 的数字(但只生成一次这个数字)或另一种方法。 我可以看到使用列表有一些建议,但这些建议似乎集中在列表中包含的大量数字上,而我目前只有 20 个问题。
我已经复制了我当前使用的代码。
private void Verify(int Question)
{
switch (Question)
{
case 1:
if (checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked && !checkBox4.Checked)
{
MessageBox.Show("Correct - Well Done");
//This was a test to see if I could assign a random number which works but the number could then appear again meaning the user gets the same question
Random random = new Random();
QuestionCount = random.Next(0, 21);
QuestionSelection(QuestionCount);
//SelectLabel(QuestionCount);
ClearcheckBox();
}
else
{
MessageBox.Show("No - It was Sunguard");
Application.Exit();
}
break;
case 2:
if (checkBox3.Checked && !checkBox2.Checked && !checkBox1.Checked && !checkBox4.Checked)
{
//this method was the original where it just adds 1 to QuestionCount and works it way through the switch statement for the questions.
MessageBox.Show("Correct - Well Done");
QuestionCount++;
QuestionSelection(QuestionCount);
//SelectLabel(QuestionCount);
ClearcheckBox();
}
else
{
MessageBox.Show("No - It's to look at a students details");
Application.Exit();
}
}
}
【问题讨论】:
-
您要查找的关键字是“shuffle”。也就是说,随机重新排序列表而不进行替换。您想重新排列问题列表(在您的代码中,问题似乎只是由
ints 标识,因此是ints 的列表)。