-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.java
98 lines (88 loc) · 3.14 KB
/
quiz.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
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
97
98
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class quiz {
private static Timer timer;
private static final int TIMEOUT = 5000; // Time in milliseconds
private static boolean timeUp;
private static int correctAnswers;
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("WELCOME TO THE QUIZ :)");
System.out.println("YOU HAVE ONLY 5 SECS TO ANSWER EACH QUESTION");
correctAnswers = 0;
question1(sc);
}
System.out.println("THE ANSWERS YOU HAVE ANSWERED CORRECTLY ARE "+ correctAnswers );
}
private static void question1(Scanner sc) {
System.out.println("Q1. WHO WAS THE FOUNDER OF CODSOFT?"); ///QUESTION 1
System.out.println("1. STANLY WALCA");
System.out.println("2. ELON MUSK");
System.out.println("3. SUNDAR PICHAI");
startTimer();
int answer1 = sc.nextInt();
stopTimer();
if (!timeUp) {
if (answer1 == 1) {
System.out.println("YAY! YOUR ANSWER IS CORRECT");
correctAnswers++;
} else {
System.out.println("I AM SORRY YOU ARE WRONG");
}
}
question2(sc);
}
private static void question2(Scanner sc) {
System.out.println("Q2. CODSOFT WAS ESTABLISHED IN THE YEAR?"); ///QUESTION 2
System.out.println("1. 2014");
System.out.println("2. 2015");
System.out.println("3. 2016");
startTimer();
int answer2 = sc.nextInt();
stopTimer();
if (!timeUp) {
if (answer2 == 2) {
System.out.println("YAY! YOUR ANSWER IS CORRECT");
correctAnswers++;
} else {
System.out.println("I AM SORRY YOU ARE WRONG");
}
}
question3(sc);
}
private static void question3(Scanner sc) {
System.out.println("Q3. JAVA WAS INTRODUCED IN THE YEAR?"); ///QUESTION3
System.out.println("1. 1995");
System.out.println("2. 1996");
System.out.println("3. 1994");
startTimer();
int answer3 = sc.nextInt();
stopTimer();
if (!timeUp) {
if (answer3 == 1) {
System.out.println("YAY! YOUR ANSWER IS CORRECT");
correctAnswers++;
} else {
System.out.println("I AM SORRY YOU ARE WRONG");
}
}
System.out.println("QUIZ HAS ENDED");
}
private static void startTimer() {
timeUp = false; ///TIMER
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
timeUp = true;
System.out.println("TIME'S UP! LET'S MOVE ON TO THE NEXT QUESTION\nPRESS ANY NUMBER AND THEN CLICK ON ENTER.");
timer.cancel();
}
}, TIMEOUT);
}
private static void stopTimer() {
if (timer != null) {
timer.cancel();
}
}
}