-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizApplication.java
More file actions
53 lines (42 loc) · 1.63 KB
/
QuizApplication.java
File metadata and controls
53 lines (42 loc) · 1.63 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
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class QuizApplication {
private static Scanner scanner = new Scanner(System.in);
private static Timer timer = new Timer();
private static int timeLimitInSeconds = 60;
private static boolean timeIsUp = false;
public static void main(String[] args) {
System.out.println("Welcome to the Quiz Application!");
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
timeIsUp = true;
timer.cancel();
System.out.println("\nTime's up!");
}
};
timer.schedule(timerTask, timeLimitInSeconds * 1000);
// Quiz questions
System.out.println("Question 1: What is the capital of India?");
String answer1 = scanner.nextLine();
System.out.println("Question 2: What is 2 + 2?");
int answer2 = scanner.nextInt();
// Checking answers
checkAnswers(answer1, answer2);
// Stop the timer if it's still running
timer.cancel();
}
private static void checkAnswers(String answer1, int answer2 ) {
if (!timeIsUp) {
// Check answers
if (answer1.equalsIgnoreCase("Delhi") && answer2 == 4 ) {
System.out.println("Congratulations! You got all answers correct!");
} else {
System.out.println("Sorry, incorrect answers ! TRY AGAIN ....");
}
} else {
System.out.println("Sorry, you ran out of time!");
}
}
}