|
6 | 6 | public class Calc { |
7 | 7 | private static final int MIN = 0; |
8 | 8 | private static final int MAX = 100; |
| 9 | + |
9 | 10 | public static void startGame() { |
10 | 11 | String[][] roundsData = new String[Engine.ROUNDS][2]; |
11 | 12 | String description = "What is the result of the expression?"; |
12 | 13 |
|
13 | 14 | for (int i = 0; i < Engine.ROUNDS; i++) { |
14 | 15 | int num1 = Utils.generateNumber(MIN, MAX); |
15 | 16 | int num2 = Utils.generateNumber(MIN, MAX); |
16 | | - char mathSymbol = getRandomMathSymbol(); |
| 17 | + char[] mathSymbols = {'+', '-', '*'}; |
| 18 | + int index = Utils.generateNumber(0, 2); |
17 | 19 |
|
18 | | - String question = num1 + " " + mathSymbol + " " + num2; |
| 20 | + String question = num1 + " " + mathSymbols[index] + " " + num2; |
19 | 21 |
|
20 | 22 | roundsData[i][0] = question; |
21 | | - roundsData[i][1] = String.valueOf(getExpressionResult(mathSymbol, num1, num2)); |
| 23 | + roundsData[i][1] = String.valueOf(calculate(mathSymbols[index], num1, num2)); |
22 | 24 | } |
23 | 25 | Engine.run(description, roundsData); |
24 | 26 | } |
25 | 27 |
|
26 | | - private static char getRandomMathSymbol() { |
27 | | - char[] mathSymbols = {'+', '-', '*'}; |
28 | | - int index = Utils.generateNumber(0, 2); |
29 | | - return mathSymbols[index]; |
30 | | - } |
31 | | - |
32 | | - private static int getExpressionResult(char mathSymbol, int numOne, int numTwo) { |
33 | | - return switch (mathSymbol) { |
34 | | - case '+' -> numOne + numTwo; |
35 | | - case '-' -> numOne - numTwo; |
36 | | - case '*' -> numOne * numTwo; |
37 | | - default -> 0; |
| 28 | + private static int calculate(char operator, int num1, int num2) { |
| 29 | + return switch (operator) { |
| 30 | + case '+' -> num1 + num2; |
| 31 | + case '-' -> num1 - num2; |
| 32 | + case '*' -> num1 * num2; |
| 33 | + default -> throw new RuntimeException("Unknown operator: " + operator); |
38 | 34 | }; |
39 | 35 | } |
40 | 36 | } |
0 commit comments