gpt4 book ai didi

c# - 我怎样才能在开关盒中只走一次?

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:58 24 4
gpt4 key购买 nike

我用 C# 做了一个测验,我的问题是用随机数随机问的。问题是我只想在每种情况下去一次。我怎样才能做到这一点?

感谢您的回答。

Random rdmNb = new Random();
int rdm1 = rdmNb.Next(1, 11);

switch (rdm1)
{
case 1:
lblQuesttion.Text = strQ1;
break;
case 2:
lblQuesttion.Text = strQ2;
break;
case 3:
lblQuesttion.Text = strQ3;
break;
case 4:
lblQuesttion.Text = strQ4;
break;
case 5:
lblQuesttion.Text = strQ5;
break;
case 6:
lblQuesttion.Text = strQ6;
break;
case 7:
lblQuesttion.Text = strQ7;
break;
case 8:
lblQuesttion.Text = strQ8;
break;
case 9:
lblQuesttion.Text = strQ9;
break;
case 10:
lblQuesttion.Text = strQ10;
break;
}

最佳答案

列出你的问题

List<string> questions = new List<string>()
{
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);

无需切换....

请务必在驱动您选择下一个问题的循环外声明随机变量。如本例所示

// 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
List<string> questions;

public MyForm()
{
InitializeComponent();
LoadQuestions();
}

private void LoadQuestions()
{
questions = new List<string>()
{
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.....
}

}

关于c# - 我怎样才能在开关盒中只走一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34565330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com