0% found this document useful (0 votes)
6 views

java learning journal 1

This Java program implements a five-question multiple-choice quiz that evaluates user answers and calculates a final score. It uses a Scanner to gather input, checks answers with if statements, and displays the percentage score upon completion. The program is based on concepts from 'Introduction to programming using java' by D. J. Eck.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

java learning journal 1

This Java program implements a five-question multiple-choice quiz that evaluates user answers and calculates a final score. It uses a Scanner to gather input, checks answers with if statements, and displays the percentage score upon completion. The program is based on concepts from 'Introduction to programming using java' by D. J. Eck.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Input:

import java.util.Scanner;

public class class1 {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int score = 0;

System.out.println("Welcome to the Quiz Game!");


System.out.println("Answer the following questions:");

// 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:

Welcome to the Quiz Game!

Answer the following questions:

Question 1: How many hours are there in a day?

A. 24

B. 20
C. 15

D. 10

Your answer: a

Question 2: What is the largest city in Afghanistan?

A. Kabul

B. Tehran

C. London

D. Tokyo

Your answer: a

Question 3: Which country is located in Africa?

A. Afghanistan

B. Egypt

C. Iran

D. America

Your answer: b

Question 4: How many days are there in a week?

A. 5 days

B. 2 days

C. 6 days

D. 7 days

Your answer: d

Question 5: What is the largest country in the world?

A. Russia

B. Iran

C. Pakistan
D. Afghanistan

Your answer: a

Quiz completed! Your final score is 100.0%.

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:

Here is the screenshots of the code.

You might also like