This repository was archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuiz.java
More file actions
82 lines (64 loc) · 2.22 KB
/
Quiz.java
File metadata and controls
82 lines (64 loc) · 2.22 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
package cs1120;
import javax.swing.JOptionPane;
//main method
public class Quiz {
// two static member variables
static int nQuestions = 0;
static int nCorrect = 0;
public static void main(String[] args) {
//list of questions
String question = "When programming language Java was created?\n";
question += "A. 1995 \n";
question += "B. 2001 \n";
question += "C. 1991 \n";
question += "D. 1967 \n";
question += "E. 1987 \n";
String question2 = "When the Bitcoin was invented?\n";
question2 += "A. 2005 \n";
question2 += "B. 2009 \n";
question2 += "C. 2015 \n";
question2 += "D. 2019 \n";
question2 += "E. 2010 \n";
String question3 = "What is the biggest plannet in solar system? \n";
question3 += "A. Mercur \n";
question3 += "B. Earth \n";
question3 += "C. Mars \n";
question3 += "D. Saturn \n";
question3 += "E. Jupiter \n";
//running check method to display question
check(question, "C");
check(question2, "B");
check(question3, "E");
// show the number of questions and correct answers
JOptionPane.showMessageDialog(null, nCorrect + " correct out of " + nQuestions + " questions ");
}
//check for valid answer
private static boolean isValidAnswer(String answer) {
if (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D") || answer.equals("E")) {
return true;
}
return false;
}
// ask method that takes a question and return answer in upper case
static String ask(String question) {
String answer = JOptionPane.showInputDialog(question).toUpperCase();
while(!isValidAnswer(answer)) {
JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");
answer = ask(question);
}
return answer;
}
// method check that take question and check the correct answer
static void check(String question, String correctAnswer) {
// increment questions each time
nQuestions ++;
String answer = ask(question);
if (answer.equals(correctAnswer)) {
// increment correct answers for correct answers
nCorrect ++;
JOptionPane.showMessageDialog(null, "Correct!");
}else {
JOptionPane.showMessageDialog(null, "Incorrect. The correct answer is "+ correctAnswer);
}
}
}