【发布时间】: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然后在你的字典中而不是<int, string>保持<int, Quiz> -
用户应该键入答案,还是从列表中选择? (如多项选择题)
-
另外,如果你使用上面的
class Quiz,答案可以是一个字符串,或者为了便于检查,可以是一个可供选择的答案列表 -
@Cid 它应该输入答案,然后如果它是正确的,它应该显示一个新问题,如果它是错误的,它应该关闭应用程序