20

我正在创建一个小测验控制台应用程序。我列了一个清单,里面有 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)]);
    }
}
4

7 回答 7

22

您确定要删除一个问题并返回其余问题吗?你不应该只选择一个吗?像这样的东西:

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]);
}
于 2013-10-11T12:45:52.820 回答
6

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

//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);
}
于 2014-02-19T01:23:11.760 回答
5

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

    }
}
于 2013-10-11T12:44:53.030 回答
3

尝试这样的事情

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需要初始化对象。

于 2013-10-11T12:47:19.890 回答
1

“名称‘字符串’在当前上下文中不存在”

我假设你想要

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. 那将假定有一种static Next方法Random并非如此。因此,我在上面展示了如何创建和使用实例。请注意,您不应该在方法本身中创建它,因为它是用当前时间播种的。如果您非常快速地调用此方法,您将经常获得相同的“随机”值。

查看备注部分的 msdn了解更多详细信息。

于 2013-10-11T12:45:40.507 回答
1

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

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

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

于 2013-10-11T12:47:09.183 回答
0

你有几个小错误。

你需要一个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);
}
于 2013-10-11T12:46:19.947 回答