【问题标题】:Making a program that ask the user 5 questions in random without duplicate the questions [duplicate]制作一个随机询问用户5个问题而不重复问题的程序[重复]
【发布时间】:2013-01-29 17:12:41
【问题描述】:

可能重复:
Randomize a List<T> in C#

我想制作一个程序,随机问用户 5 个问题而不重复问题,如果问题是正确的,如果错误则继续下一个问题,直到他给出正确的答案,这就是我的代码已经写了,但是还是有一些问题,比如有重复,当用户输入错误的答案时,它只会停止一次,然后程序就关闭了! 现在如何防止重复相同的问题,如果他输入错误的值不要继续下一个问题或程序关闭?

static void Main()
{
    next:
    Random question = new Random();
    int x = question.Next(5);
    string[] array = new string[5];
    array[0] = "-What is the capital of France";
    array[1] = "-What is the capital of Spain";
    array[2] = "-What is the captial of Russia";
    array[3] = "-What is the capital of Ukraine";
    array[4] = "-What is the capital of Egypt";

    Console.WriteLine(array[x]);

    string[] answer = new string[5];
    answer[0] = "Paris";
    answer[1] = "Madrid";
    answer[2] = "Moscow";
    answer[3] = "Kiev";
    answer[4] = "Cairo";

    string a = Console.ReadLine();

    if (a == answer[x])
    {
        Console.WriteLine("It's True \n*Next Question is:");
        goto next;
    }
    else
        Console.WriteLine("It's False \n*Please Try Again.");

    Console.ReadLine();
}

【问题讨论】:

  • 请提出具体问题(如有必要,请提出多个问题),而不是“这不起作用,请修复”类型之一。也请删除此goto。
  • 天啊,请不要使用 GOTO。 That's the Devil himself
  • @ken2k .. 我已经更新了这个问题并这样做了! :)
  • @HighCore .. 我可以用什么代替?!
  • @user2022523 Flow Control

标签: c#


【解决方案1】:

您可以使用 LINQ 随机排列所问问题的索引

Random random = new Random();
var indexes = Enumerable.Range(0,array.Length).OrderBy(i => random.Next());

foreach(var index in indexes)
{
    Console.WriteLine(array[index]);
    do 
    {        
        string a = Console.ReadLine();

        if (a == answer[index]) {                
          Console.WriteLine("It's True\n");
          break;
        }

        Console.WriteLine("It's False \n*Please Try Again.");
    }
    while(true);
}

解释:

Enumerable.Range(0,array.Length) 将返回从零开始的整数值范围:0, 1, 2, 3, 4。接下来,这些数字将按随机数排序(即随机排列)。它可能导致这些数字的任何组合,例如3, 0, 1, 4, 2.


顺便说一句,最好采用 OOP 方式并将相关数据(问题文本和答案)和逻辑(定义答案是否正确)放在一个地方:

public class Question
{
    public string Text { get; set; }
    public string Answer { get; set; }

    public bool IsCorrect(string answer)
    {
        return String.Compare(answer, Answer, true) == 0;
    }
}

有了这个问题类,你的所有代码都将更具可读性:

var questions = new List<Question>()
{
    new Question { Text = "What is the capital of France?", Answer = "Paris" },
    new Question { Text = "What is the capital of Spain?", Answer = "Madrid" },
    new Question { Text = "What is the capital of Russia?", Answer = "Moscow" },
    new Question { Text = "What is the capital of Ukraine?", Answer = "Kiev" },
    new Question { Text = "What is the capital of Egypt?", Answer = "Cairo" }
};

Random random = new Random();

// randomizing is very simple in this case
foreach (var question in questions.OrderBy(q => random.Next()))
{
    Console.WriteLine(question.Text);

    do
    {
        var answer = Console.ReadLine();
        if (question.IsCorrect(answer))
        {
            Console.WriteLine("It's True");
            break;
        }

        Console.WriteLine("It's False. Please try again.");
    }
    while (true);
}

下一步是实现Survey 类,它将进行提问、阅读答案和显示摘要。

【讨论】:

  • 还有更多efficient algorithm 可以随机播放列表。但是,此解决方案可以快速实施。
  • 在这种情况下,绝对不是。我也很想知道你为什么被否决。
  • @lazyberezovsky - 哈哈!我完全同意你的看法!而且,不客气。
  • @lazyberezovsky .. 谢谢!老兄,它太有用了,而且很有效:) 太棒了
  • @lazyberezovsky .. 我当然会,但我不能在“7 小时”之前这样做,所以我一定会 :)
【解决方案2】:

除非绝对必要,否则不要使用 goto。

public class Program
{
    private static List<KeyValuePair<string, string>> questions = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string,string>("-What is the capital of France", "Paris"),
        new KeyValuePair<string,string>("-What is the capital of Spain", "Madrid"),
        new KeyValuePair<string,string>("-What is the captial of Russia", "Moscow"),
        new KeyValuePair<string,string>("-What is the capital of Ukraine", "Kiev"),
        new KeyValuePair<string,string>("-What is the capital of Egypt", "Cairo"),
    };

    static void Main()
    {
        var questions = ShuffleQuestions();

        foreach(var question in questions)
        {
            bool done = false;
            while(!done)
            {
                Console.WriteLine(question.Key);
                string a = Console.ReadLine();

                if (a == question.Value)
                {
                    Console.WriteLine("It's True \n*Next Question is:");
                    done = true;
                }
                else
                    Console.WriteLine("It's False \n*Please Try Again.");
            }
        }

        Console.ReadLine();
    }

    private IEnumerable<KeyValuePair<string, string>> ShuffleQuestions()
    {
        var list = questions;
        var random = new Random();  
        int items = list.Count;  
        while (items > 1) {  
            items--;  
            int nextItem = random.Next(items + 1);  
            var value = list[nextItem];  
            list[nextItem] = list[items];  
            list[items] = value;  
        }

        return list;
    }
}

【讨论】:

    【解决方案3】:

    您可以创建一个无序的整数数组,代表提出问题的顺序:

    // Create question order
    var order = Enumerable.Range(0, array.Length).ToArray();
    for (int i = order.Length - 1; i > 1; --i)
    {
        var randomIndex = rnd.Next(i);
        var temp = order[randomIndex];
        order[randomIndex] = order[i];
        order[i] = temp;
    }
    
    // Ask the questions in shuffled order
    foreach(int questionIndex in order)
    {
        Console.Write(array[questionIndex]);
        bool answeredCorrectly = Console.ReadLine() == answer[questionIndex];
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 2018-07-21
      • 1970-01-01
      • 2014-06-14
      • 2020-06-20
      相关资源
      最近更新 更多