0% found this document useful (0 votes)
2 views3 pages

Java Assignment 1

Uploaded by

madandolataiwo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Java Assignment 1

Uploaded by

madandolataiwo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

This is a Java program that implements a simple quiz game according to your specifications.

The
program includes five multiple-choice questions, each with four options (A, B, C, D). Users
input their answers, and the program computes the final score based on correct responses. The
code uses a combination of `if` and `switch` statements to handle user input and answer
validation.

```java
Import [Link];

Public class QuizGame {


Public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

// Array of questions
String[] questions = {
“What is the capital of France?\n A: Paris\n B: London\n C: Rome\n D: Madrid”,
“Which language is primarily spoken in Brazil?\n A: English\n B: Portuguese\n C:
Spanish\n D: French”,
“What is the chemical symbol for Water?\n A: CO2\n B: H2O\n C: O2\n D: NaCl”,
“What year did the first man land on the moon?\n A: 1965\n B: 1969\n C: 1971\n D:
1973”,
“Who wrote ‘Hamlet’?\n A: Charles Dickens\n B: William Shakespeare\n C: Leo Tolstoy\
n D: Mark Twain”
};

// Array of correct answers


Char[] answers = {‘A’, ‘B’, ‘B’, ‘B’, ‘B’};

Int score = 0;

// Loop through each question


For (int I = 0; I < [Link]; i++) {
[Link](questions[i]); // Display the current question
[Link](“Enter your answer (A, B, C, D): “);
String input = [Link]().toUpperCase(); // Read user input and convert to
uppercase

// Validate input and ensure it’s a single character A, B, C, or D


If ([Link]() == 1 && “ABCD”.contains(input)) {
Char userAnswer = [Link](0);

// Compare user’s answer with the correct answer


If (userAnswer == answers[i]) {
Score++; // Increment score if answer is correct
}
} else {
[Link](“Invalid input. Please enter A, B, C, or D.”);
i--; // Decrement I to repeat the question for a valid answer
continue;
}
}

[Link]();

// Calculate and display the final score as a percentage


Double percentage = 100.0 * score / [Link];
[Link](“Your final score is: %.2f%% (%d out of %d correct)\n”, percentage,
score, [Link]);
}
}
```

Explanation:

Import Scanner: We use the `Scanner` class to read user input.


Questions and Answers: Defined using arrays; questions are stored as strings and correct
answers are stored as characters.
User Interaction: The program iterates over each question, displaying it to the user and asking
for an input.
Input Validation: Input is converted to uppercase to ensure consistency. The program checks if
the input is one of ‘A’, ‘B’, ‘C’, ‘D’. If not, the question is repeated.
Score Calculation: The score is incremented whenever a user’s answer matches the correct
answer.
Result Display: At the end of the quiz, the score is displayed as both a count of correct answers
and a percentage.

This program fulfills all requirements laid out in your assignment and includes necessary error
checking to handle invalid inputs effectively. Test this program with various inputs to ensure its
robustness and accuracy.
Reference:
Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition. Licensed
under CC 4.0.
[Link]
choice-questions-then-performs-inpu
How do you create a simple quiz in Java? [Link]
quiz-in-Java

You might also like