1

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.

I already made the random generator, the questions and answers (tkey and tvalue)

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.

This is what I have right now

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?

An0d
  • 213
  • 2
  • 12
Morris
  • 189
  • 1
  • 13
  • Would you like to map the real answer to the question or would you like to store the answer of the user next to the question? – Marnix Nov 07 '19 at 08:36
  • 4
    Create a `class Quiz` with `public string Question {get;set;}` and `public string Answer {get;set;}` then in your dictionary instead of `` keep `` – Nawed Nabi Zada Nov 07 '19 at 08:37
  • Should the user type the answer, or select it from a list ? (like in a multiple choice questionaire) – Cid Nov 07 '19 at 08:38
  • @NawedNabiZada I would rather create a `Dictionary` that stores a question as key with it's answer as value, then select a random key – Cid Nov 07 '19 at 08:39
  • 1
    Also, if you use the `class Quiz` as above the answer could either be a string or, easier for checking, a list of alternate answers to enable multiple choice – Peter Smith Nov 07 '19 at 08:40
  • 1
    @Cid it should type the answer and then if its right it shoudl display a new question and if it is wrong it should close the application – Morris Nov 07 '19 at 08:42

3 Answers3

1

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.

In example (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 !");
        }
    }
}
Cid
  • 14,968
  • 4
  • 30
  • 45
0

Put answer for each key from Questions.

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

store the specific answer by same random index of question into another variable

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

Display the question to read answer from console

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

Now compare the input with value of 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");
}
Kawsar Hamid
  • 514
  • 3
  • 13
0

You should create a "Question" class containing the question text, a list of answers and the correct answer. So your Questions dictionary should be:

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