Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C从列表中选择随机元素_C#_Random - Fatal编程技术网

C# C从列表中选择随机元素

C# C从列表中选择随机元素,c#,random,C#,Random,我正在创建一个小测验控制台应用程序。 我列出了一个清单,里面有3个问题。 如何让程序随机选择一个问题并在控制台中打印出来 我尝试了一些不同的代码,但由于某些原因,似乎无法让它工作。 这是我最后一次尝试的代码,我从这个站点的另一个用户那里得到的,但是我得到了错误: 名称“string”在当前上下文中不存在 由于quick.Questions.main返回void,因此return关键字后面不能跟有对象表达式 下面是我尝试的最后一段代码: class Questions { public s

我正在创建一个小测验控制台应用程序。 我列出了一个清单,里面有3个问题。 如何让程序随机选择一个问题并在控制台中打印出来

我尝试了一些不同的代码,但由于某些原因,似乎无法让它工作。 这是我最后一次尝试的代码,我从这个站点的另一个用户那里得到的,但是我得到了错误:

名称“string”在当前上下文中不存在

由于quick.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;

    }

您需要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]);

    }
}
名称“string”在当前上下文中不存在

我想你想要

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。这将假定存在随机的静态Next方法,但事实并非如此。因此,我在上面展示了如何创建和使用实例。请注意,您不应该在方法本身中创建它,因为它是使用当前时间进行种子设定的。如果你很快调用这个方法,你会经常得到相同的随机值


查看更多详细信息。

是否确实要删除一个问题并返回其余问题? 你不应该只选择一个吗?像这样的事情:

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]);
}

你有几个小错误

您需要对兰德对象的引用。你看的是字符串而不是问题。您正在删除图元而不是选择它

试试这个:

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);
}

像这样的东西可能是你想要的:

private Random rng;
T PickRandom<T>(List<T> list)
{
    return list[rng.NextInt(list.Count)];
}

您可以在列表中调用它,从中获取一个随机元素。

试试类似的方法

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);
}

在使用字符串而不是问题的地方有一个输入错误。另外,需要初始化随机对象。

对于其他搜索者的好处:如果您想要一个消耗列表,以便确保以随机方式使用所有项目,请执行以下操作:

//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);
}

还有字符串。计数应替换为问题。计数和随机。下一步。。。由新随机。下一个…谢谢阿列克西。更正。@AlexeyMitev:new Random。Next有一个陷阱,即如果在短时间内重复调用它,它将不会创建不同的数字。您如何调用您的方法?由于它目前是一种空类型,您必须省略返回值questions duplicate=>var randomQuestion=questions[new Random.Nextquestions.Count];首先确保该列表不为空。可能的副本pretty sure Random没有静态的Nextmethod@WeylandYutani:谢谢,相应地编辑了我的答案。我认为Random.NextInt在c语言中不存在,而在Java语言中不存在。如果我是正确的,那么代码应该是rng.Nextlist.Count,而不是rng.NextIntlist.Count.Note:对的调用应该将list.Count作为第二个参数,而不是list.Count-1作为Random.nextin的第二个参数。建议的编辑应被拒绝。
//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);
}