带有随机问题的C#应用程序无法添加答案 [英] C# application with random questions can't add answers
问题描述
我有一个项目,我想用随机的问题和答案进行随机测验.我使用字典来完成此任务,并成功地提出了问题和答案,并随机显示了一个问题.
I have a project where I want to make a random quiz with random questions and answers. I used a dictionary to do this and I succeeded in making the questions and answers and in displaying a random one.
现在,我想为每个提出的问题添加具体答案.我想这也应该通过字典来完成,但是我不知道该如何做.
Now I want to add specific answers per question asked. I figured this should be done by a dictionary also but I don't know how to make this work.
我已经制作了随机生成器,问题和答案(tkey和tvalue)
I already made the random generator, the questions and answers (tkey and tvalue)
因此,现在我只需要添加一个部分,您可以为每个问题提供特定的答案,以及一种检测问题是对还是错的方法,这可以通过if和else语句来完成,但我的经验不足知道如何实现所有这些功能.
So now I only need to add a part where you have specific answers per questions and a way to detect if the questions are wrong or right, this can be done by a if and a else statement but I'm not experienced enough to know how to implement all these functions.
这就是我现在拥有的
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();
}
}
有人可以通过告诉我/帮助我为这些问题添加具体答案的方式来帮助我吗?
Can someone help me by telling me / helping me make a way to add specific answers to these questions?
推荐答案
您可以使用 Dictionary< string,string>
将问题存储为键和答案为值
You can use a Dictionary<string, string>
to store the questions as key and the answer as value
Then, get the list of keys and shuffle it, to finally iterate through that list.
在示例中(弄乱下面的代码):
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 !");
}
}
}
这篇关于带有随机问题的C#应用程序无法添加答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!