C# 从列表中选择随机元素
本文关键字:随机 元素 选择 列表 | 更新日期: 2023-09-27 18:35:08
我正在创建一个小测验控制台应用程序。我列出了一个包含 3 个问题的清单。如何让程序随机选择一个问题并在控制台中打印出来?
我已经尝试了一些不同的代码,但由于某种原因似乎无法让它工作。这是我尝试的最后一个代码,是从该站点的其他用户那里获得的,但是我收到错误:
名称"字符串"在当前上下文中不存在。
"由于Quiz.Questions.main()
返回 void,因此 return 关键字后不得跟对象表达式"。
这是我尝试的最后一段代码:
class Questions
{
public static void main()
{
var questions = new List<string>{
"question1",
"question2",
"question3"};
int index = Random.Next(strings.Count);
questions.RemoveAt(index);
return questions;
}
}
谢谢大家的回复。我已经通过创建一个数组而不是一个列表来解决我的问题。这是我现在的代码:
class Questions
{
public static void main()
{
string[] questions = new string[3];
questions[0] = "question1";
questions[1] = "question2";
questions[2] = "question3";
Random rnd = new Random();
Console.WriteLine(questions[rnd.Next(0,2)]);
}
}
您确定要删除问题并返回其余问题吗?你不应该只选择一个吗?像这样:
public static void main()
{
var random = new Random();
var questions = new List<string>{
"question1",
"question2",
"question3"};
int index = random.Next(questions.Count);
Console.WriteLine(questions[index]);
}
你需要一个 System.Console.WriteLine 语句。
class Questions
{
public static void main()
{
var questions = new List<string>{
"question1",
"question2",
"question3"};
int index = Random.Next(questions.Count);
System.Console.WriteLine(questions[index]);
}
}
对于其他搜索者的好处: 如果你想要一个耗尽列表,以确保你以随机的方式使用所有项目,那么这样做:
//use the current time to seed random so it's different every time we run it
Random rand = new Random(DateTime.Now.ToString().GetHashCode());
var list = new List<string>{ "item1", "item2", "item3"};
//keep extracting from the list until it's depleted
while (list.Count > 0) {
int index = rand.Next(0, list.Count);
Console.WriteLine("Rand Item: " + list[index]);
list.RemoveAt(index);
}
试试这样的事情
public static void main()
{
var questions = new List<string>{
"question1",
"question2",
"question3"};
Random rnd = new Random();
int index = rnd.Next(questions.Count)
string question = questions[index];
questions.RemoveAt(index); // Are you sure you neex to remove?
System.Console.WriteLine(question);
}
您使用string
而不是questions
的地方有一个拼写错误。 此外,Random
对象需要启动。
像这样的东西可能是你想要的:
private Random rng;
T PickRandom<T>(List<T> list)
{
return list[rng.NextInt(list.Count)];
}
您可以在列表中调用它以从中获取随机元素。
"名称'字符串'在当前上下文中不存在"
我假设你想要
int index = random.Next(questions.Count); // according to the lower-case random see last paragraph
而不是
int index = Random.Next(strings.Count);
由于没有名称为 strings
的变量,并且您无论如何都想删除一个问题。
此外,不能从void
方法返回某些内容。因此,创建一个返回列表的:
private Random random = new Random();
List<string> GetRemoveQuestion(List<string> questions)
{
int index = random.Next(questions.Count);
questions.RemoveAt(index);
return questions;
}
编辑:最后但并非最不重要的一点是,您不能使用 Random.Next
.这将假定Random
中有一种static
Next
方法,但事实并非如此。因此,我在上面展示了如何创建和使用实例。请注意,您不应该创建它 i 方法本身,因为它是用当前时间播种的。如果你非常快速地调用此方法,你通常会得到相同的"随机"值。
更多详细信息,请查看备注部分的 msdn。
你有几个小错误。
您需要对Rand
对象的引用。您正在查看strings
而不是questions
.您正在删除元素,而不是选择它。
试试这个:
void Main()
{
Random rand = new Random();
var questions = new List<string>{
"question1",
"question2",
"question3"};
int index = rand.Next(questions.Count);
return questions[index];
// If you want to use Linq then
// return questions.Skip(index).Take(1);
}