c# - 从 M 集合中取出 n 个唯一对象

标签 c# asp.net logic combinations

我想知道如何在 C# 中完成此任务。例如;

I got 10 questions from which 3 is to be displayed to user for them to type the answer. How can i make the program generate 3 questions that are non-repeating(unique) assuming the 10 questions that we are starting with is unique.

我在 asp.net 应用程序中使用逻辑,并且允许在下次刷新页面时显示同一组问题,所以这对我来说没问题。

最佳答案

为您的问题实例使用一个列表,并随机选择一个(按索引)。然后将其从列表中删除并重复。像这样;

    static void Main(string[] args)
    {
        List<string> questions = new List<string>();
        for (int i = 0; i < 10; i++)
            questions.Add("Question " + i);

        Random r = new Random();
        for (int i = 0; i < 3; i++)
        {
            int nextQuestion = r.Next(0, questions.Count);
            Console.WriteLine(questions[nextQuestion]);
            questions.RemoveAt(nextQuestion);
        }
    }

关于c# - 从 M 集合中取出 n 个唯一对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12775145/

相关文章:

c# - Entity Framework Core 使用 Unique 检查添加多行

asp.net - 空白 WebResource.axd

javascript - 我想根据时间和持续时间显示 json 中的元素,并且间隔被 settimeout 中断

arduino - 评估一个值是增加还是减少(arduino)

c# - 如何定期将 c# FileStream 刷新到磁盘?

c# - 尝试插入数据表的数组值时出现 SQL 语法错误

c# - 在 ASP.NET Core 中使用 MimeMapping

ASP.NET Enter 键触发不需要的按钮

Jquery从表列表中隐藏单个表中的图像

java - 如何查找字符串中字符之间的最长距离