-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain1.dart
75 lines (61 loc) · 1.89 KB
/
main1.dart
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
import 'dart:io';
import 'dart:math';
void main() {
final quiz = Quiz();
print("Welcome to the Quiz App!");
print("Press Enter to start...");
stdin.readByteSync(); // Wait for a single character (Enter key)
quiz.startQuiz();
print("Thank you for playing the Quiz App!");
}
class Quiz {
final List<Question> questions = [
Question("What is the capital of France?", "Paris"),
Question("What is the largest planet in our solar system?", "Jupiter"),
// Add more questions here...
];
void startQuiz() {
final randList = generateRandomIndices(questions.length);
int score = 0;
for (var index in randList) {
final question = questions[index];
print(question.question);
final userAnswer = promptForAnswer();
if (userAnswer == question.answer) {
print("Correct!");
score++;
} else {
print("Incorrect. The correct answer is: ${question.answer}");
}
}
final percentage = (score / questions.length * 100).toStringAsFixed(2);
print("Total Correct Answers: $score/${questions.length}");
print("Percentage: $percentage%");
if (score > questions.length / 2) {
print("Congratulations! You did a great job!");
} else {
print("Keep practicing to improve your score!");
}
}
String promptForAnswer() {
print("Your answer: ");
return stdin.readLineSync()?.trim() ?? '';
}
List<int> generateRandomIndices(int length) {
final max = length;
final rand = Random();
final randList = <int>[];
while (randList.length < 20) {
final randomNumber = rand.nextInt(max);
if (!randList.contains(randomNumber)) {
randList.add(randomNumber);
}
}
return randList;
}
}
class Question {
final String question;
final String answer;
Question(this.question, this.answer);
}