-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathQuizApp.java
41 lines (34 loc) · 1.25 KB
/
QuizApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class QuizApp {
public static void main(String[] args) {
// Define some sample questions and answers
String[] questions = {
"What is the capital of France?",
"What is the largest planet in our solar system?",
"Who wrote Romeo and Juliet?"
};
String[] answers = {
"Paris",
"Jupiter",
"William Shakespeare"
};
// Initialize the quiz
int score = 0;
Scanner scanner = new Scanner(System.in);
// Iterate through the questions
for (int i = 0; i < questions.length; i++) {
System.out.println(questions[i]);
String userAnswer = scanner.nextLine();
if (userAnswer.equalsIgnoreCase(answers[i])) {
System.out.println("Correct!\n");
score++;
} else {
System.out.println("Incorrect. The correct answer is: " + answers[i] + "\n");
}
}
// Display the final score
System.out.println("Quiz complete! Your score is: " + score + " out of " + questions.length);
// Close the scanner
scanner.close();
}
}