创建一个问题列表
Listquestions = new List () { strQ1,strQ2,strQ3,strQ4,strQ5,strQ6,strQ7,strQ8,strQ9,strQ10 };
然后更改您的随机生成以从列表中查找问题
Random rdmNb = new Random(); int rdm1 = rdmNb.Next(0, questions.Count); lblQuesttion.Text = questions[rdm1];
并从列表中删除问题
questions.RemoveAt(rdm1);
无需开关....
一定要在循环外声明Random变量,以驱动您选择下一个问题.如在这个例子中
// Declare globally the random generator, not inside the question loop Random rdmNb = new Random(); while (questions.Count > 0) { int rdm1 = rdmNb.Next(0, questions.Count); string curQuestion = questions[rdm1]; questions.RemoveAt(rdm1); lblQuestion.Text = curQuestion; ... ?code to handle the user input? }
编辑
在表单内声明并初始化具有全局范围的问题列表.
public class MyForm : Form { // Declaration at global level Listquestions; public MyForm() { InitializeComponent(); LoadQuestions(); } private void LoadQuestions() { questions = new List () { strQ1,strQ2,strQ3,strQ4,strQ5,strQ6,strQ7,strQ8,strQ9,strQ10 }; // In future you could change this method to load your questions // from a file or a database..... } }