java learning journal 1
java learning journal 1
import java.util.Scanner;
// Question 1
System.out.println("Question 1: How many hours are there in a day?");
System.out.println("A. 24");
System.out.println("B. 20");
System.out.println("C. 15");
System.out.println("D. 10");
System.out.print("Your answer: ");
String answer1 = input.nextLine();
if (answer1.equalsIgnoreCase("A")) {
score++;
}
// Question 2
System.out.println("Question 2: What is the largest city in
Afghanistan?");
System.out.println("A. Kabul");
System.out.println("B. Tehran");
System.out.println("C. London");
System.out.println("D. Tokyo");
System.out.print("Your answer: ");
String answer2 = input.nextLine();
if (answer2.equalsIgnoreCase("A")) {
score++;
}
// Question 3
System.out.println("Question 3: Which country is located in
Africa?");
System.out.println("A. Afghanistan");
System.out.println("B. Egypt");
System.out.println("C. Iran");
System.out.println("D. America");
System.out.print("Your answer: ");
String answer3 = input.nextLine();
if (answer3.equalsIgnoreCase("B")) {
score++;
}
// Question 4
System.out.println("Question 4: How many days are there in a week?");
System.out.println("A. 5 days");
System.out.println("B. 2 days");
System.out.println("C. 6 days");
System.out.println("D. 7 days");
System.out.print("Your answer: ");
String answer4 = input.nextLine();
if (answer4.equalsIgnoreCase("D")) {
score++;
}
// Question 5
System.out.println("Question 5: What is the largest country in the
world?");
System.out.println("A. Russia");
System.out.println("B. Iran");
System.out.println("C. Pakistan");
System.out.println("D. Afghanistan");
System.out.print("Your answer: ");
String answer5 = input.nextLine();
if (answer5.equalsIgnoreCase("A")) {
score++;
}
// Final score
double percentage = (double) score / 5 * 100;
System.out.println("Quiz completed! Your final score is " +
percentage + "%.");
}
}
Output:
A. 24
B. 20
C. 15
D. 10
Your answer: a
A. Kabul
B. Tehran
C. London
D. Tokyo
Your answer: a
A. Afghanistan
B. Egypt
C. Iran
D. America
Your answer: b
A. 5 days
B. 2 days
C. 6 days
D. 7 days
Your answer: d
A. Russia
B. Iran
C. Pakistan
D. Afghanistan
Your answer: a
Explanation:
This Java program creates a simple five-question multiple-choice quiz. It gathers user input using
a Scanner, checks each answer against the correct solution, and keeps track of the user's score. After all
questions are answered, the program calculates and displays the user's percentage score.
The program works by presenting each question one by one. It then evaluates the user's input using an
`if` statement. This statement determines whether the user's answer is correct and updates the score
accordingly. This process repeats for each of the five questions.
Once the quiz is finished, the program calculates the percentage score by dividing the total points
earned by the number of questions. The final score is then displayed along with a message indicating the
quiz is complete.
The `if` statement is a fundamental programming construct. It allows the program to make decisions
based on whether a condition is true or false. In this case, the `if` statement determines if the user's
answer is correct, allowing the program to update the score accordingly (Eck, 2022).
References:
Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition. Licensed under CC
4.0. https://math.hws.edu/javanotes/.
Appendix: