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

本文关键字:问题 一个 随机 程序 用户 | 更新日期: 2023-09-27 17:56:36

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

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

您可以使用

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类,它将包含提问,阅读答案并显示摘要。

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

您可以创建一个随机的整数数组,这些数组表示在以下位置提问的顺序:

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