From the course: Learning Java 11

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Sample solution: Multiple choice

Sample solution: Multiple choice - Java Tutorial

From the course: Learning Java 11

Start my 1-month free trial

Sample solution: Multiple choice

(upbeat music) - [Instructor] Let's walk through how to create a multiple-choice program in Java. From the previous lesson we started off with this template. The first thing we need to do is come up with our question and answer choices. Our question will be what is the largest planet in our solar system. The answer choices will be Earth, Jupiter, and Saturn. We set the correct answer choice to be equal to choice two because Jupiter is the largest planet in the solar system. I could make choice three have the value Jupiter and then choice two have the value Saturn but I would need to remember to update correct answer to choice three instead of choice two. Next we'll ask our question. Then we'll give the user some answer choices. To retrieve the users input we create a scanner using system.n and you use the .next operation. Now for the control flow portion. We can use an if statement to check if the users input matches the correct answer. If the condition is true we'll print out a…

Contents