Skip to content

Commit 1bd3f11

Browse files
committed
upd magic number fixing
1 parent c9bdb5b commit 1bd3f11

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
lines changed

app/src/main/java/hexlet/code/Calc.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55

66
public class Calc {
77
public static void gameLogic() {
8+
final int RND_MAX_VALUE = 100;
9+
final int MATH_SYMBOL_MAX_VALUE = 3;
810
Engine.userGreetings();
911
Scanner sc = new Scanner(System.in);
1012
char[] mathSymbols = {'+', '-', '*'};
1113
int result = 0;
1214
System.out.println("What is the result of the expression?\n");
13-
while (Engine.questionCounter != 3) {
15+
while (Engine.questionCounter != Engine.MAX_QUESTIONS) {
1416
Random randNum = new Random();
15-
int randMathSymbol = randNum.nextInt(3);
16-
int num1 = randNum.nextInt(100);
17-
int num2 = randNum.nextInt(100);
17+
int randMathSymbol = randNum.nextInt(MATH_SYMBOL_MAX_VALUE);
18+
int num1 = randNum.nextInt(RND_MAX_VALUE);
19+
int num2 = randNum.nextInt(RND_MAX_VALUE);
1820
System.out.println("Question: " + num1 + " " + mathSymbols[randMathSymbol] + " " + num2);
1921
result = switch (mathSymbols[randMathSymbol]) {
2022
case '+' -> num1 + num2;
@@ -33,7 +35,7 @@ public static void gameLogic() {
3335
System.out.println("Let's try again, " + Engine.userName + "!");
3436
break;
3537
}
36-
if (Engine.questionCounter == 3) {
38+
if (Engine.questionCounter == Engine.MAX_QUESTIONS) {
3739
System.out.println("Congratulations, " + Engine.userName + "!");
3840
}
3941

app/src/main/java/hexlet/code/Engine.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class Engine {
77
public static String userName = "";
88
public static int questionCounter = 0;
9+
public static final int MAX_QUESTIONS = 3;
910

1011
public static void userGreetings() {
1112
Scanner sc = new Scanner(System.in);

app/src/main/java/hexlet/code/EvenGame.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static void gameLogic() {
77
Scanner sc = new Scanner(System.in);
88
Engine.userGreetings();
99
System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
10-
while (Engine.questionCounter != 3) {
10+
while (Engine.questionCounter != Engine.MAX_QUESTIONS) {
1111
Random rand = new Random();
1212
int randNum = rand.nextInt(100);
1313
System.out.println("Question: " + randNum);
@@ -23,7 +23,7 @@ public static void gameLogic() {
2323
System.out.println("Let's try again, " + Engine.userName + "!");
2424
break;
2525
}
26-
if (Engine.questionCounter == 3) {
26+
if (Engine.questionCounter == Engine.MAX_QUESTIONS) {
2727
System.out.println("Congratulations, " + Engine.userName + "!");
2828
}
2929
}

app/src/main/java/hexlet/code/GCD.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
public class GCD {
77
public static void gameLogic() {
8+
final int MAX_VALUE = 100;
89
Engine.userGreetings();
910
System.out.println("Find the greatest common divisor of given numbers.");
1011
Scanner sc = new Scanner(System.in);
1112
int result;
12-
while (Engine.questionCounter != 3) {
13+
while (Engine.questionCounter != Engine.MAX_QUESTIONS) {
1314
Random randNum = new Random();
14-
int num1 = randNum.nextInt(100);
15-
int num2 = randNum.nextInt(100);
15+
int num1 = randNum.nextInt(MAX_VALUE);
16+
int num2 = randNum.nextInt(MAX_VALUE);
1617
System.out.println("Question: " + num1 + " " + num2);
1718
int answer = sc.nextInt();
1819
result = Engine.gcdGenerate(num1, num2);
@@ -26,7 +27,7 @@ public static void gameLogic() {
2627
System.out.println("Let's try again, " + Engine.userName + "!");
2728
break;
2829
}
29-
if (Engine.questionCounter == 3) {
30+
if (Engine.questionCounter == Engine.MAX_QUESTIONS) {
3031
System.out.println("Congratulations, " + Engine.userName + "!");
3132
}
3233
}

app/src/main/java/hexlet/code/Prime.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
public class Prime {
77
public static void gameLogic() {
8+
final int RND_NUM_MAX = 1000;
89
Scanner sc = new Scanner(System.in);
910
Random randNum = new Random();
1011
Engine.userGreetings();
1112
System.out.println("Answer 'yes' if given number is prime. Otherwise answer 'no'.");
12-
while (Engine.questionCounter != 3) {
13-
int num = randNum.nextInt(2, 1000);
13+
while (Engine.questionCounter != Engine.MAX_QUESTIONS) {
14+
int num = randNum.nextInt(2, RND_NUM_MAX);
1415
boolean isPrime = true;
1516
System.out.println("Question: " + num);
1617
for (int i = 2; i <= Math.sqrt(num); i++) {
@@ -31,7 +32,7 @@ public static void gameLogic() {
3132
System.out.println("Let's try again, " + Engine.userName + "!");
3233
break;
3334
}
34-
if (Engine.questionCounter == 3) {
35+
if (Engine.questionCounter == Engine.MAX_QUESTIONS) {
3536
System.out.println("Congratulations, " + Engine.userName + "!");
3637
}
3738
}

app/src/main/java/hexlet/code/Progression.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55

66
public class Progression {
77
public static void gameLogic() {
8+
final int ARR_LENGTH = 10;
9+
final int STEP_MAX = 10;
10+
final int HIDE_ELEMENT_MAX = 9;
11+
final int FIRST_NUM_MAX = 100;
812
Scanner sc = new Scanner(System.in);
9-
int[] progression = new int[10];
13+
int[] progression = new int[ARR_LENGTH];
1014
Random randNum = new Random();
1115

1216
Engine.userGreetings();
1317
System.out.println("What number is missing in the progression?");
14-
while (Engine.questionCounter != 3) {
15-
int progressionStep = randNum.nextInt(1, 10);
16-
int randHideElement = randNum.nextInt(0, 9);
17-
int firstNum = randNum.nextInt(1, 100);
18+
while (Engine.questionCounter != Engine.MAX_QUESTIONS) {
19+
int progressionStep = randNum.nextInt(1, STEP_MAX);
20+
int randHideElement = randNum.nextInt(0, HIDE_ELEMENT_MAX);
21+
int firstNum = randNum.nextInt(1, FIRST_NUM_MAX);
1822
progression[0] = firstNum;
1923
for (int i = 1; i < progression.length; i++) {
2024
progression[i] = progression[i - 1] + progressionStep;
@@ -41,7 +45,7 @@ public static void gameLogic() {
4145
System.out.println("Let's try again, " + Engine.userName + "!");
4246
break;
4347
}
44-
if (Engine.questionCounter == 3) {
48+
if (Engine.questionCounter == Engine.MAX_QUESTIONS) {
4549
System.out.println("Congratulations, " + Engine.userName + "!");
4650
}
4751
}

0 commit comments

Comments
 (0)