【问题标题】:C# application with random questions can't add answers带有随机问题的 C# 应用程序无法添加答案
【发布时间】:2020-03-03 19:07:55
【问题描述】:

我有一个项目,我想用随机问题和答案进行随机测验。我用字典来做这件事,我成功地制作了问题和答案,并显示了一个随机的。

现在我想为每个问题添加具体答案。我认为这也应该由字典完成,但我不知道如何使这项工作。

我已经做了随机生成器,问题和答案(tkey和tvalue)

所以现在我只需要添加一个部分,在其中您对每个问题都有特定的答案,以及一种检测问题是错误还是正确的方法,这可以通过 if 和 else 语句来完成,但我经验不足了解如何实现所有这些功能。

这就是我现在拥有的

class Program
{
    static public void Main()
    {
        //maak een dictionary aan
        Console.WriteLine("Quiz");

        Dictionary<int, string> Questions = new Dictionary<int, string>();

        //voeg vragen toe 
        //key koppelen aan vraag
        Questions.Add(11, "Vraag1?");
        Questions.Add(12, "Vraag2?");
        Questions.Add(13, "Vraag3?");
        Questions.Add(14, "Vraag4?");
        Questions.Add(15, "Vraag5?");

        Dictionary<int, string> answers = new Dictionary<int, string>();

        List<int> keylist = new List<int>(); // string naar int converten
        keylist = Questions.Keys.ToList();

        Random rand = new Random(); //maak random aan
        int countKeys = Questions.Keys.Count();
        int randomIndex = rand.Next(0, countKeys);

        Console.WriteLine(Questions.ElementAt(randomIndex).Key); //geef een random index en de gekoppelde key daaraan

        string input = Console.ReadLine();

        Console.WriteLine("You answered: " + input);

        Console.ReadKey();
    }
} 

有人可以通过告诉我/帮助我为这些问题添加具体答案来帮助我吗?

【问题讨论】:

  • 您想将真实答案映射到问题,还是将用户的答案存储在问题旁边?
  • public string Question {get;set;}public string Answer {get;set;} 创建一个class Quiz 然后在你的字典中而不是&lt;int, string&gt; 保持&lt;int, Quiz&gt;
  • 用户应该键入答案,还是从列表中选择? (如多项选择题)
  • 另外,如果你使用上面的class Quiz,答案可以是一个字符串,或者为了便于检查,可以是一个可供选择的答案列表
  • @Cid 它应该输入答案,然后如果它是正确的,它应该显示一个新问题,如果它是错误的,它应该关闭应用程序

标签: c# .net


【解决方案1】:

您应该创建一个“问题”类,其中包含问题文本、答案列表和正确答案。所以你的问题字典应该是:

Dictionary<int, Question> Questions = new Dictionary<int, Question>();

【讨论】:

    【解决方案2】:

    您可以使用Dictionary&lt;string, string&gt; 将问题存储为key,将答案存储为value

    然后,获取list of keysshuffle 它,最终遍历该列表。

    例如(fiddle to the below code):

    private static Random rng = new Random();  
    
    public static void Shuffle<T>(this IList<T> list)  
    {  
        int n = list.Count;  
        while (n > 1)
        {  
            n--;  
            int k = rng.Next(n + 1);  
            T value = list[k];  
            list[k] = list[n];  
            list[n] = value;  
        }  
    }
    
    public static void Main(string[] args)
    {
        var quizz = new Dictionary<string, string>
        {
            { "What is the answer to the Ultimate Question of Life, the Universe, and Everything ?", "42" },
            { "What is your name ?", "Sir Arthur, king of the Britons" },
            { "What is your quest ?", "To seek the Holy Grail" },
            { "What is the air-speed velocity of an unladen swallow?", "What do you mean ? An African or European swallow ?" }
        };
        var questions = quizz.Keys.ToList();
        questions.Shuffle();
    
        foreach (var question in questions)
        {
            Console.WriteLine(question);
            if (Console.ReadLine() != quizz[question])
            {
                Console.WriteLine("You failed");
                Console.ReadLine();
                break;
            }
            else
            {
                Console.WriteLine("Nice !");
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      为问题中的每个键输入答案。

      foreach(var item in Questions) {
          answers.Add(item.Key, "answer")
      }
      

      将相同随机问题索引的特定答案存储到另一个变量中

      var askingQuestion = Questions.ElementAt(randomIndex);
      var answerOfAskingQuestion = answers.ElementAt(randomIndex);
      

      显示问题以从控制台读取答案

      Console.WriteLine(answerOfAskingQuestion.value);
      string input = Console.ReadLine();
      

      现在将输入与 answerOfAskingQuestion 的值进行比较

      if (input.ToLower().Equals(answerOfAskingQuestion.Value.ToLower()) {
          // given answer is correct
          // display "your answer is correct"
          Console.WriteLine("Your answer is correct");
      } else { 
          // answer is wrong
          // display "incorrect"
          Console.WriteLine("Your answer is wrong");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-23
        • 1970-01-01
        • 1970-01-01
        • 2017-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多