Welcome back.
In this video, what we want to achieve is that we display a question and the answers.
Let the user enter the right answer or a answer, any answer, and then go to the next question.
And therefore what we're going to build is another method that will take care of starting the quiz and
handling quiz questions, or displaying the quiz questions as well as answers.
So I'm going to make this public public void start quiz.
And this method will take care of displaying something like welcome to the quiz for example.
Welcome to the quiz.
And then we can go ahead and we need to know at which question we are.
So I'm going to say we start at one.
So this will be to display question numbers.
The idea is that we display at which question we currently are okay.
Then we're going to require a for each loop which will go through all questions.
So question question in questions okay.
So it will go through every single question inside of our questions array.
This one here.
And then what we'll do it will display the question number as well as the question.
So here I'm just going to say question.
And then whatever the question number is okay.
So this is just for visibility purposes.
And I'm going to increment question number straight away as well.
So for this one moment it's question one.
And then the next iteration of this for each it will be question two.
So then at this point we can go ahead and display our question.
So display question.
And which question do we want to display.
Well this one here.
So the one from our iteration whatever item of our questions we're currently at.
So that's what this for each loop does.
Right.
And then we can go ahead and get the user choice.
So int user choice will be get user choice.
So we're using our user choice method.
This one here get user choice.
So we're basically saying hey give me the user choice like we did in this display question here.
You see here that we also got the user question.
So we can now get rid of this code here by the way because we're going to basically use a better logic.
So in this for each loop we're checking if question dot is correct.
Answer user choice.
So the cool thing is now that we are actually using this method, that we created this boolean method
which checks whether whatever we entered as an ID is actually correct or not.
We have.
This is correct answer method that we're now using.
So we're using question dot is correct answer.
And we're giving the user choice.
So in get user choice.
And this method we're taking care of the user choice actually being a valid option.
And here we are checking whether this valid option was correct.
And if this is correct because this is correct answer.
If you hover over it you see it returns a bool.
That's what we defined here.
So it's going to return true or false.
So if the answer was correct we're going to display correct.
So here CW and we're going to say correct.
And then if we're not correct then we can display wrong.
For example not very friendly.
But yeah.
So here we can say wrong.
And then we can even say what the correct answer would have been.
So the correct answer was.
And then this is a little more tricky here where how do we get the actually correct answer.
Do you have an idea.
Well we need to use question dot correct answer index.
And we could go even further.
We could actually say question.
So if you really want to display the actual answer and not the option, you could go and say, now this
will go like this.
So question question dot answers.
And then inside of square brackets question correct answer index.
So in this line what we're saying look at the answers options.
So out of those options here take the one that has the correct answer index.
So we're taking the index of the correct answer and displaying that particular answer.
So we're saying if the correct answer index is one then go and check which of those answers has the
index one it's Berlin.
So display Berlin.
So it would say the correct answer was Berlin for example.
And that is pretty much our start quiz logic.
So it will now go through all of our quiz questions.
So now while we're at it we can make sure that we make display question private.
Why?
Because.
We don't need to display the question outside of our quiz.
Our quiz is taking care of like this.
Quiz class is taking care of displaying questions, so we don't need to expose the method to other classes,
which is why I made it private.
However, now we obviously need to modify our program here.
So in our program CSS file.
Now instead of saying my quiz display questions we can start the quiz.
So now we're saying my quiz start quiz.
Now in order for us to have a few questions I'm going to need to add more questions.
So for example, I could say something like what is two plus two?
And then the answer possibilities would be something like three, four, five and six.
And the correct answer index would be one as well in this case.
So that's pretty much our second question.
Now let's actually see if we get from one question to the next.
So what is the capital of Germany.
And I'm going to say Berlin.
It says correct.
And what is two plus two.
And this time I'm actually going to enter a wrong answer.
So I'm going to say two plus two is four.
And then it's going to say wrong.
The correct answer was four.
So yeah that's a tricky one.
So in this particular case it would have been better to display the index instead of displaying the
correct answer.
Let me run this again so that you're not getting confused here okay.
So what is the capital of Germany I'm going to say one and it says wrong.
The correct answer was Berlin.
So here you see it does make sense.
Now in this particular second case.
Now you can play around with it, change it to your liking.
But let's say two plus two is actually four.
And I'm going to say it's 3 or 1.
Whatever it says wrong.
The correct answer was four.
Okay.
So in this particular case if I entered four then it becomes confusing.
But if I enter one, then it says the answer is four.
So it meant this four and not this four.
Now as I said, you can either display the index or you can display the result itself.
Um, the change where you would change that would be inside of our quiz, inside of this part here,
where you either display the answer itself or you display the index of the answer.
So you could have said the correct answer index if you used the index only, or you can use the correct
answer.
Good.
So we're almost done.
Now we just need to display the result okay.
So currently we can go through questions and we can finish the questions.
But we don't get to the point where we are done so to speak.
Right.
So here like I'm closing the application.
So there is no proper closure for our application.
So let's build that in the next video.