-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathQuizForm1.cs
More file actions
202 lines (170 loc) · 6.42 KB
/
MathQuizForm1.cs
File metadata and controls
202 lines (170 loc) · 6.42 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MathQuiz
{
public partial class mathquizForm1 : Form
{
// Create a Random object called randomizer
// to generate random numbers.
Random randomizer = new Random();
// These integer variables store the numbers
// for the addition problem.
int addend1;
int addend2;
// These integer variables store the numbers
// for the subtraction problem.
int minuend;
int subtrahend;
// These integer variables store the numbers
// for the multiplication problem.
int multiplicand;
int multiplier;
// These integer variables store the numbers
// for the division problem.
int dividend;
int divisor;
// This integer variable keeps track of the
// remaining time.
int timeLeft;
/// Start the quiz by filling in all of the problems
/// and starting the timer.
public void StartTheQuiz()
{
// Fill in the addition problem.
// Generate two random numbers to add.
// Store the values in the variables 'addend1' and 'addend2'.
addend1 = randomizer.Next(51);
addend2 = randomizer.Next(51);
// Convert the two randomly generated numbers
// into strings so that they can be displayed
// in the label controls.
plusLeftLabel.Text = addend1.ToString();
plusRightLabel.Text = addend2.ToString();
// 'addition' is the name of the NumericUpDown control.
// This step makes sure its value is zero before
// adding any values to it.
addition.Value = 0;
// Fill in the subtraction problem.
minuend = randomizer.Next(1, 101);
subtrahend = randomizer.Next(1, minuend);
minusLeftLabel.Text = minuend.ToString();
minusRightLabel.Text = subtrahend.ToString();
soustraction.Value = 0;
// Fill in the multiplication problem.
multiplicand = randomizer.Next(2, 11);
multiplier = randomizer.Next(2, 11);
timesLeftLabel.Text = multiplicand.ToString();
timesRightLabel.Text = multiplier.ToString();
multiplication.Value = 0;
// Fill in the division problem.
divisor = randomizer.Next(2, 11);
int temporaryQuotient = randomizer.Next(2, 11);
dividend = divisor * temporaryQuotient;
dividedLeftLabel.Text = dividend.ToString();
dividedRightLabel.Text = divisor.ToString();
division.Value = 0;
// Start the timer, reset BackColor property.
timeLeft = 30;
timeLabel.Text = "30 secondes";
timeLabel.BackColor = Color.Empty;
timer1.Start();
}
/// Check the answer to see if the user got everything right.
/// True if the answer's correct, false otherwise.
private bool CheckTheAnswer()
{
if ((addend1 + addend2 == addition.Value)
&& (minuend - subtrahend == soustraction.Value)
&& (multiplicand * multiplier == multiplication.Value)
&& (dividend / divisor == division.Value))
return true;
else
return false;
}
public mathquizForm1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void startButton_Click(object sender, EventArgs e)
{
StartTheQuiz();
startButton.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (CheckTheAnswer())
{
// If CheckTheAnswer() returns true, then the user
// got the answer right. Stop the timer
// and show a MessageBox.
timer1.Stop();
MessageBox.Show("Toutes les réponses sont justes!",
"Félicitations!");
startButton.Enabled = true;
}
if (timeLeft > 0)
{
// Display the new time left
// by updating the Time Left label.
timeLeft = timeLeft - 1;
timeLabel.Text = timeLeft + " secondes";
}
else
{
// If the user ran out of time, stop the timer, show
// a MessageBox, and fill in the answers.
timer1.Stop();
timeLabel.Text = "Temps écoulé!";
MessageBox.Show("Vous n'avez pas réussi à terminer dans le temps disponible!", "Désolé, c'est perdu!");
addition.Value = addend1 + addend2;
soustraction.Value = minuend - subtrahend;
multiplication.Value = multiplicand * multiplier;
division.Value = dividend / divisor;
startButton.Enabled = true;
}
if (timeLeft < 6)
{
// When only five seconds remain in a quiz, turn the timeLabel control red
// by setting its BackColor property.
timeLabel.BackColor = Color.Red;
}
else
{
// Reset the color when the quiz is over.
timeLabel.BackColor = Color.Empty;
}
}
private void answer_Enter(object sender, EventArgs e)
{
// Select the whole answer in the NumericUpDown control.
NumericUpDown answerBox = sender as NumericUpDown;
if (answerBox != null)
{
int lengthOfAnswer = answerBox.Value.ToString().Length;
answerBox.Select(0, lengthOfAnswer);
}
}
private void timeLabel_Click(object sender, EventArgs e)
{
}
private void MathQuizForm1_Load(object sender, EventArgs e)
{
}
}
}