forked from kujian/githubTrending
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestionGame.sk
155 lines (137 loc) · 5.06 KB
/
QuestionGame.sk
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.*;
public class QuizGame {
private static Map<String, Quiz> quizzes = new HashMap<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter a command: (create, take, view, list, exit)");
String command = scanner.nextLine();
if (command.equals("create")) {
createQuiz(scanner);
} else if (command.equals("take")) {
takeQuiz(scanner);
} else if (command.equals("view")) {
viewQuiz(scanner);
} else if (command.equals("list")) {
listQuizzes();
} else if (command.equals("exit")) {
break;
} else {
System.out.println("Invalid command.");
}
}
}
private static void createQuiz(Scanner scanner) {
System.out.println("Enter the name of the quiz:");
String quizName = scanner.nextLine();
Quiz quiz = new Quiz(quizName);
System.out.println("Enter the number of questions:");
int numQuestions = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numQuestions; i++) {
System.out.println("Enter the question:");
String question = scanner.nextLine();
System.out.println("Enter the number of choices:");
int numChoices = Integer.parseInt(scanner.nextLine());
List<String> choices = new ArrayList<>();
for (int j = 0; j < numChoices; j++) {
System.out.println("Enter choice " + (j+1) + ":");
String choice = scanner.nextLine();
choices.add(choice);
}
System.out.println("Enter the index of the correct choice:");
int correctChoice = Integer.parseInt(scanner.nextLine()) - 1;
quiz.addQuestion(new Question(question, choices, correctChoice));
}
quizzes.put(quizName, quiz);
System.out.println("Quiz created.");
}
private static void takeQuiz(Scanner scanner) {
System.out.println("Enter the name of the quiz:");
String quizName = scanner.nextLine();
Quiz quiz = quizzes.get(quizName);
if (quiz == null) {
System.out.println("Quiz not found.");
return;
}
int score = 0;
for (int i = 0; i < quiz.getNumQuestions(); i++) {
Question question = quiz.getQuestion(i);
System.out.println("Question " + (i+1) + ": " + question.getQuestion());
List<String> choices = question.getChoices();
for (int j = 0; j < choices.size(); j++) {
System.out.println((j+1) + ": " + choices.get(j));
}
System.out.println("Enter your answer:");
int userAnswer = Integer.parseInt(scanner.nextLine()) - 1;
if (userAnswer == question.getCorrectChoice()) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Incorrect. The correct answer is " + (question.getCorrectChoice()+1) + ".");
}
}
System.out.println("Your score is " + score + " out of " + quiz.getNumQuestions() + ".");
}
private static void viewQuiz(Scanner scanner) {
System.out.println("Enter the name of the quiz:");
String quizName = scanner.nextLine();
Quiz quiz = quizzes.get(quizName);
if (quiz == null) {
System.out.println("Quiz not found.");
return;}
System.out.println("Quiz: " + quiz.getName());
for (int i = 0; i < quiz.getNumQuestions(); i++) {
Question question = quiz.getQuestion(i);
System.out.println("Question " + (i+1) + ": " + question.getQuestion());
List<String> choices = question.getChoices();
for (int j = 0; j < choices.size(); j++) {
System.out.println((j+1) + ": " + choices.get(j));
}
System.out.println("Answer: " + (question.getCorrectChoice()+1));
}
}
private static void listQuizzes() {
System.out.println("Quizzes:");
for (String quizName : quizzes.keySet()) {
System.out.println("- " + quizName);
}
}
}
class Quiz {
private String name;
private List<Question> questions = new ArrayList<>();
public Quiz(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void addQuestion(Question question) {
questions.add(question);
}
public Question getQuestion(int index) {
return questions.get(index);
}
public int getNumQuestions() {
return questions.size();
}
}
class Question {
private String question;
private List<String> choices;
private int correctChoice;
public Question(String question, List<String> choices, int correctChoice) {
this.question = question;
this.choices = choices;
this.correctChoice = correctChoice;
}
public String getQuestion() {
return question;
}
public List<String> getChoices() {
return choices;
}
public int getCorrectChoice() {
return correctChoice;
}
}