-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathQuizRandom.java
More file actions
152 lines (128 loc) · 5.18 KB
/
mathQuizRandom.java
File metadata and controls
152 lines (128 loc) · 5.18 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
package java-projects;
import java.util.*;
public class mathQuizRandom {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
while (true) {
System.out.print("Enter your number of items on your a quiz: ");
int size = input.nextInt();
String[] questions = new String[size];
int[] answers = new int[size];
int[] userAnswers = new int[size];
String[] Identify = new String[size];
int score = 0;
int arith = 0;
int divNum = 0;
System.out.println("Choose difficulty \n1. Easy \n2. Normal \n3. Hard");
int diff = input.nextInt();
switch (diff) {
case 1:
System.out.println("You've choosed easy mode!");
break;
case 2:
System.out.println("You've choosed normal mode!");
break;
case 3:
System.out.println("You've choosed hard mode!");
break;
default:
System.out.println("Error: Wrong choice!");
break;
}
for (int i = 0; i < size; i++) {
System.out.print(i + 1 + ". ");
if (diff == 1) {
arith = rand.nextInt(2) + 1;
divNum = 9;
} else if (diff == 2) {
arith = rand.nextInt(3) + 1;
divNum = 99;
} else if (diff == 3) {
arith = rand.nextInt(4) + 1;
divNum = 500;
}
if (arith == 1) {
int num1 = rand.nextInt(divNum) + 1;
int num2 = rand.nextInt(divNum) + 1;
int total = num1 - num2;
answers[i] = total;
questions[i] = num1 + " - " + num2;
System.out.printf("%d - %d = ", num1, num2);
userAnswers[i] = input.nextInt();
if (userAnswers[i] == answers[i]) {
System.out.print("Correct!\n");
Identify[i] = "Correct";
score += 1;
} else if (userAnswers[i] != answers[i]) {
System.out.print("Wrong!\n");
Identify[i] = "Wrong";
}
} else if (arith == 2) {
int num1 = rand.nextInt(divNum) + 1;
int num2 = rand.nextInt(divNum) + 1;
int total = num1 + num2;
answers[i] = total;
questions[i] = num1 + " + " + num2;
System.out.printf("%d + %d = ", num1, num2);
userAnswers[i] = input.nextInt();
if (userAnswers[i] == answers[i]) {
System.out.print("Correct!\n");
Identify[i] = "Correct";
score += 1;
} else if (userAnswers[i] != answers[i]) {
System.out.print("Wrong!\n");
Identify[i] = "Wrong";
}
} else if (arith == 3) {
int num1 = rand.nextInt(500) + 1;
int num2 = rand.nextInt(500) + 1;
int total = num1 * num2;
answers[i] = total;
questions[i] = num1 + " x " + num2;
System.out.printf("%d x %d = ", num1, num2);
userAnswers[i] = input.nextInt();
if (userAnswers[i] == answers[i]) {
System.out.print("Correct!\n");
Identify[i] = "Correct";
score += 1;
} else if (userAnswers[i] != answers[i]) {
System.out.print("Wrong!\n");
Identify[i] = "Wrong";
}
} else if (arith == 4) {
int num1 = rand.nextInt(divNum) * 2;
int num2 = rand.nextInt(divNum) * 2;
int total = num1 / num2;
answers[i] = Math.round(total);
questions[i] = num1 + " / " + num2;
System.out.printf("%d / %d = ", num1, num2);
userAnswers[i] = input.nextInt();
if (userAnswers[i] == answers[i]) {
System.out.print("Correct!\n");
Identify[i] = "Correct";
score += 1;
} else if (userAnswers[i] != answers[i]) {
System.out.print("Wrong!\n");
Identify[i] = "Wrong";
}
}
}
for (int j = 0; j < Identify.length; j++) {
if (Identify[j] == "Correct") {
System.out.printf("%d. %s (%s = %d)\n", j + 1, Identify[j], questions[j], userAnswers[j]);
} else if (Identify[j] == "Wrong") {
System.out.printf("%d. %s (%s = %d) Answer: %d\n", j + 1, Identify[j], questions[j], userAnswers[j], answers[j]);
}
}
System.out.println("Score: " + score);
System.out.print("Try again? Y/N: ");
char tryAgain = input.next().toUpperCase().charAt(0);
if (tryAgain == 'Y') {
continue;
} else if (tryAgain == 'N') {
break;
}
}
}
}