0

I have to create a Quiz which prints questions in a random order. I have written out all the questions in if statements, and if 10 questions have been answered I want all the questions to be displayed with a proper answer to them and user input answer to compare. I do not know yet how to make it so the code goes to the next if statement after 1 is finished.

How do I finish 1 if statement (question) and the random number generator points to another if and so on.

static void Main(string[] args)
    {
        string answer1;
        string answer2;
        string answer3;
        string answer4;
        string answer5;
        string answer6;
        string answer7;
        string answer8;
        string answer9;
        string answer10;
        int answeredQs = 0;

        Random rnd = new Random();
        int questionNum = rnd.Next(1,10);
        Console.WriteLine("Question Number: " + questionNum);

            if (questionNum == 1)
            {
                Console.WriteLine("What is a CPU?");
                answer1 = Console.ReadLine();
                answeredQs = +1;
            }

            if (questionNum == 2)
            {
                Console.WriteLine("What does 'RAM' stand for?");
                answer2 = Console.ReadLine();
                answeredQs = answeredQs + 1;
            }

            if (questionNum == 3)
            {
                Console.WriteLine("What is RAM?");
                answer3 = Console.ReadLine();
                answeredQs = answeredQs + 1;
            }

            if (questionNum == 4)
            {
                Console.WriteLine("How do you measure how fast a processor is?");
                answer4 = Console.ReadLine();
                answeredQs = answeredQs + 1;
            }

            if (questionNum == 5)
            {
                Console.WriteLine("What is an ALU & what does it do?");
                answer5 = Console.ReadLine();
                answeredQs = answeredQs + 1;
            }

            if (questionNum == 6)
            {
                Console.WriteLine("What is a register?");
                answer6 = Console.ReadLine();
                answeredQs = answeredQs + 1;
            }

            if (questionNum == 7)
            {
                Console.WriteLine("What is EEPROM?");
                answer7 = Console.ReadLine();
                answeredQs = answeredQs + 1;
            }

            if (questionNum == 8)
            {
                Console.WriteLine("What is the difference between SRAM and DRAM?");
                answer8 = Console.ReadLine();
                answeredQs = answeredQs + 1;
            }

            if (questionNum == 9)
            {
                Console.WriteLine("What is ROM?");
                answer9 = Console.ReadLine();
                answeredQs = answeredQs + 1;
            }

            if (questionNum == 10)
            {
                Console.WriteLine("What does the Control Unit do?");
                answer10 = Console.ReadLine();
                answeredQs = answeredQs + 1;
            }

            if (answeredQs == 10)
            {
                Console.WriteLine("asdasdasd");
            }
Dominik N
  • 3
  • 3
  • Great! And what is your question ? – Fabjan Sep 28 '16 at 09:57
  • Ok I have made an adjustment; How do I finish 1 if statement (question) and the random number generator points to another if and so on. – Dominik N Sep 28 '16 at 10:01
  • Well, you can put all your questions in array and shuffle it, then take them out one by one (when they are already in a random order). You can read how to shuffle arrays [here](http://stackoverflow.com/questions/4120002/how-to-randomize-array-items-and-then-crop-the-array-by-required-percent). – Fabjan Sep 28 '16 at 10:08

1 Answers1

2

You could put your if statements in a loop (for, while...)

Also, your code could be more readable by putting the questions in an ArrayList for example (or any other "data-storing" structure, an simple array for instance).

Then, you could play on the index of the questions which are in this structure, always in a loop.

Hope this helps

Lucas Delobelle
  • 424
  • 2
  • 6
  • Okay thanks. Our Lecturer has not taught us Arrays yet, so don't really know. – Dominik N Sep 28 '16 at 10:04
  • 1
    Ok, I assume that you have seen the loops then ? This may be what you're looking for – Lucas Delobelle Sep 28 '16 at 10:05
  • We have done a bit on While, but nothing on For loops. So what you are saying is for me to use a loop I'd have to put everything into an array and the array inside the loop? – Dominik N Sep 28 '16 at 10:10
  • Not exactly. You can first set a loop with your present code, to process all the statements "one by one" if you want. But since all the questions have the same type and structure, you'll see (later I guess) that a structured data type could help you. Because then you can use the loop on this structured variable, or just loop randomly and use the index of this structured variable to access your questions – Lucas Delobelle Sep 28 '16 at 10:14