-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuiz.java
More file actions
96 lines (80 loc) · 2.85 KB
/
Quiz.java
File metadata and controls
96 lines (80 loc) · 2.85 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
class Quiz {
private List<Question> questions;
private int score;
private Scanner scanner;
private boolean timeUp;
public Quiz() {
questions = new ArrayList<>();
score = 0;
scanner = new Scanner(System.in);
}
public void addQuestion(Question question) {
questions.add(question);
}
public void start() {
System.out.println("Welcome to the Quiz Game!");
System.out.println("You'll have limited time to answer each question.");
System.out.println("Let's begin!\n");
for (int i = 0; i < questions.size(); i++) {
if (!askQuestion(questions.get(i), i + 1)) {
break;
}
}
showFinalScore();
}
private boolean askQuestion(Question question, int questionNumber) {
System.out.println("Question " + questionNumber + ":");
System.out.println(question.getQuestionText());
String[] options = question.getOptions();
for (int i = 0; i < options.length; i++) {
System.out.println((i + 1) + ". " + options[i]);
}
timeUp = false;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
timeUp = true;
System.out.println("\nTime's up!");
System.out.println("Press Enter to continue...");
}
}, question.getTimeLimit() * 1000);
System.out.print("\nYour answer (1-" + options.length + "): ");
int answer;
try {
answer = scanner.nextInt();
if (timeUp) {
System.out.println("Sorry, time's up! Moving to the next question.");
timer.cancel();
return true;
}
timer.cancel();
if (answer < 1 || answer > options.length) {
System.out.println("Invalid answer! Question skipped.");
return true;
}
if (question.isCorrect(answer - 1)) {
System.out.println("Correct! +1 point");
score++;
} else {
System.out.println("Wrong answer!");
}
} catch (Exception e) {
System.out.println("Invalid input! Quiz ended.");
return false;
}
System.out.println("\nCurrent score: " + score + "\n");
return true;
}
private void showFinalScore() {
System.out.println("\n--- Quiz Completed! ---");
System.out.println("Final Score: " + score + "/" + questions.size());
double percentage = (double) score / questions.size() * 100;
System.out.println("Percentage: " + String.format("%.1f%%", percentage));
}
}