-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuiz Program.txt
More file actions
77 lines (62 loc) · 2.31 KB
/
Copy pathQuiz Program.txt
File metadata and controls
77 lines (62 loc) · 2.31 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.*;
class QuizQuestion {
private String question;
private List<String> options;
private char correctAnswer;
public QuizQuestion(String question, List<String> options, char correctAnswer) {
this.question = question;
this.options = options;
this.correctAnswer = correctAnswer;
}
public String getQuestion() {
return question;
}
public List<String> getOptions() {
return options;
}
public char getCorrectAnswer() {
return correctAnswer;
}
}
public class Quiz {
private List<QuizQuestion> questions;
private int score;
private Scanner scanner;
public Quiz() {
questions = new ArrayList<>();
scanner = new Scanner(System.in);
score = 0;
// Add quiz questions here
questions.add(new QuizQuestion("What is the capital of France?",
Arrays.asList("A. London", "B. Paris", "C. Rome", "D. Berlin"), 'B'));
questions.add(new QuizQuestion("What is the largest planet in our solar system?",
Arrays.asList("A. Jupiter", "B. Saturn", "C. Earth", "D. Mars"), 'A'));
// Add more questions...
// Start the quiz
startQuiz();
}
private void startQuiz() {
for (int i = 0; i < questions.size(); i++) {
QuizQuestion currentQuestion = questions.get(i);
System.out.println("Question " + (i + 1) + ": " + currentQuestion.getQuestion());
for (String option : currentQuestion.getOptions()) {
System.out.println(option);
}
System.out.print("Your answer: ");
char userAnswer = scanner.next().toUpperCase().charAt(0);
if (userAnswer == currentQuestion.getCorrectAnswer()) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Incorrect! The correct answer is " + currentQuestion.getCorrectAnswer());
}
System.out.println();
}
// Display result
System.out.println("Quiz ended. Your final score is: " + score + "/" + questions.size());
scanner.close();
}
public static void main(String[] args) {
new Quiz();
}
}