-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion.pde
58 lines (46 loc) · 1.3 KB
/
Question.pde
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
class Question {
int num1, num2;
String [] operations = {" + ", " - ", " X ", " / "}; // Used to display questions
int operation; // operation index
float correctAnswer;
Question() {
}
void generateQuestion() {
// First number is always greater than second (I'm avoiding negative and decimal answers)
num1 = (int) random(1, 10);
num2 = (int) random(1, num1 + 1);
operation = (int) random(0, 4);
//operation = 0;
// Addition
if (operation == 0) {
correctAnswer = num1 + num2;
}
// Subtraction
else if (operation == 1) {
correctAnswer = num1 - num2;
}
// Multiplication
else if (operation == 2) {
correctAnswer = num1 * num2;
}
// Division
else {
correctAnswer = float(num1) / float(num2);
// Make sure answer is a whole number, otherwise, regenerate question
if (correctAnswer != round(correctAnswer)){
generateQuestion();
}
}
}
// Returns question for displaying
String getQuestion() {
return Integer.toString(num1) + operations[operation] + Integer.toString(num2) + " = ";
}
// Check if given answer is correct
boolean evaluate(int answer) {
if (answer == correctAnswer) {
return true;
}
return false;
}
}