制作一个程序,随机询问用户5个问题而不重复问题

时间:2013-01-29 17:12:41

标签: c#

  

可能重复:
  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();
}

3 个答案:

答案 0 :(得分:3)

您可以使用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课程,该课程将提出问题,阅读答案和显示摘要。

答案 1 :(得分:3)

除非绝对必要,否则不要使用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;
    }
}

答案 2 :(得分:2)

您可以创建一个整齐的整数数组,表示在以下位置提出问题的顺序:

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