-1

Section 1A:

Hello everyone, how can I get the user to specify the amount of questions? This is the part where I am confused, Basically, the program contains a bank of 100 questions (for now there's only 9 questions) and I would like to let the user specify the amount of questions they want to answer. How would I go about doing this implementing this onto the section of code below?

            var questions = new List<Question>()    
{
    new Question { Text = "Q. [Attack On Titan] Which character is the 'Rogue' Titan?", Answer = "Eren"},
    new Question { Text = "Q. [Pokemon] Which Pokemon does Ash use mostly?", Answer = "Pikachu" },
    new Question { Text = "Q. [Fairy Tail] Who raised Natsu Dragneel when he was a child?", Answer = "Igneel" },
    new Question { Text = "Q. [Death Note] What was Light's surname?", Answer = "Yagami" },
    new Question { Text = "Q. [Attack On Titan] Who was Eren's best friend?", Answer = "Armin" },
    new Question { Text = "Q. [Attack On Titan] Which character is the'Armored' Titan?", Answer = "Reiner" },
    new Question { Text = "Q. [Attack On Titan] Which character is the 'Colossal' Titan?", Answer = "Bertholt" },
    new Question { Text = "Q. [Death Note] In the series, there was always a shinigami with Light Yagami, what was their name?", Answer = "Ryuk" },
    new Question { Text = "Q. [Attack On Titan] Who gave Mikasa their red scarf?", Answer = "Eren" },
};

            Random random = new Random();
            int correctAnswers = 0;

            foreach (var question in questions.OrderBy(q => random.Next()))
            {
                Console.WriteLine(question.Text);
                string answer = "";

                do
                {
                    answer = Console.ReadLine();

                    if (question.IsCorrect(answer))
                    {
                        Console.WriteLine($"That is correct! {++correctAnswers}/100");

                        Thread.Sleep(800);

                        Console.Clear();
                    }

                    Console.WriteLine("You are incorrect.");
                    System.Threading.Thread.Sleep(40);
                }
Zen
  • 13
  • 4

1 Answers1

2

First, your user will get stuck forever with the same question unless you exit the do loop somehow. I would suggest not using the do loop at all.
Now, to answer your question:

Get the number from the user - I assume you know how Console.ReadLine works and how Int.TryParse works:

int numberOfquestions;
do
{
    Console.WriteLine("Please enter a number between 1 and 100");
} while(!int.TryParse(Console.ReadLine(), out numberOfquestions) && numberOfquestions >= 1 && numberOfquestions < 100);

Then, shuffle your list (there is a better way), and then just take the first numberOfquestions:

foreach (var question in RandomlyOrderedQuestions.Take(numberOfquestions))   
{
    // the rest of your code here...
}
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Shouldn't there be `do { Console.WriteLine("Please enter a number between 1 and 100"); } while(!int.TryParse(Console.ReadLine(), out numberOfQuestions) && numberOfQuestions >= 1 && numberOfQuestions <= 100);` ? – mrogal.ski Jan 31 '18 at 12:20
  • @m.rogalski Yes. I started to write it differently (wanted a different message for invalid input) but then changed my mind. I've edited my answer according to your suggestion. – Zohar Peled Jan 31 '18 at 12:30
  • Hey, the code you gave me works fine. I had to insert this following code: private static object RandomlyOrderedQuestions; can you assist me with this section as well please? thanks bud :) – Zen Jan 31 '18 at 12:57
  • You can use a local variable, it doesn't have to be a static one. I don't know what's "this section" you refer to. If you have another question, post it as a question. – Zohar Peled Jan 31 '18 at 13:00
  • would it be okay if i ask the question on this thread, or would i need to create a new one? – Zen Jan 31 '18 at 13:04
  • Ask a new question. SO is not a forum, there are no threads here. every question should be in it's own post. – Zohar Peled Jan 31 '18 at 13:06
  • @ZoharPeled can you help me with one thing please? This bit here: foreach (var question in >> RandomlyOrderedQuestions << keeps showing an error) .Take(numberOfquestions)) { var RandomlyOrderedQuestions = new List() { – Zen Jan 31 '18 at 13:29
  • @ZoharPeled actually, it's okay i sorted it. Thanks for the help bro – Zen Jan 31 '18 at 13:37
  • @ZoharPeled I fixed the errors, but whenever I input the amount of questions I want, for instance 5 it just doesn't do as it is told... I tried to add the shuffle list as you mentioned but I don't know where to begin with that, so can I please ask of your assistance? – Zen Jan 31 '18 at 15:46
  • You should probaby open a new question with your current code and explain what is wrong with it. – Zohar Peled Jan 31 '18 at 15:49