Skip to content

Commit ef33372

Browse files
committed
modify the code
1 parent 5b1b2d6 commit ef33372

File tree

8 files changed

+50
-49
lines changed

8 files changed

+50
-49
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class App {
1111
public static void main(String[] args) {
1212

1313
try(Scanner scanner = new Scanner(System.in)) {
14-
1514
System.out.println("Please enter the game number and press Enter.");
1615
System.out.print("1 - Greet\n2 - Even\n3 - Cacl\n4 - GCD\n5 - Progression\n6 - Prime\n0 - Exit\nYour choice: ");
1716
int usersChoise = 0;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Scanner;
44

55
public final class Cli {
6-
public static String userName;
6+
public static final StringBuilder USER_NAME = new StringBuilder();
77

88
private Cli() {
99
throw new AssertionError("Utility class instantiation prohibited");
@@ -12,7 +12,7 @@ private Cli() {
1212
public static void greetings(Scanner scanner) {
1313
System.out.println("Welcome to the Brain Games!");
1414
System.out.print("May I have your name? ");
15-
userName = scanner.nextLine();
16-
System.out.println("Hello, " + userName + "!");
15+
USER_NAME.append(scanner.nextLine());
16+
System.out.println("Hello, " + USER_NAME + "!");
1717
}
1818
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public static void runGame(int gameNumber, Scanner scanner) {
5656
scoreCounter++;
5757
}
5858
else{
59-
System.out.println(usersAnswer + " is wrong answer ;(. Correct answer was " + correctAnswer.toString() + ".\nLet\'s try again, " + Cli.userName + "!");
59+
System.out.println(usersAnswer + " is wrong answer ;(. Correct answer was " + correctAnswer + ".\nLet\'s try again, " + Cli.USER_NAME + "!");
6060
break;
6161
}
6262
if(scoreCounter == scoreToWin) {
63-
System.out.println("Congratulations, " + Cli.userName);
63+
System.out.println("Congratulations, " + Cli.USER_NAME);
6464
}
6565
}
6666
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package hexlet.code.games;
22

3+
import java.util.Random;
4+
35
public final class Calc {
4-
private static int minValue = 0;
5-
private static int maxValue = 2;
6-
private static int operationNumber;
7-
private static int correctAnswer = 0;
8-
private static int numberOne;
9-
private static int numberTwo;
10-
6+
117
private Calc() {
128
throw new AssertionError("Utility class instantiation prohibited");
139
}
@@ -18,9 +14,11 @@ public static void startGameCalc() {
1814
}
1915

2016
public static String generateCorrectAnswer() {
21-
numberOne = (int) (Math.random()*100);
22-
numberTwo = (int) (Math.random()*100);
23-
operationNumber = (int) (Math.random()*(maxValue-minValue+1)+minValue);
17+
Random random = new Random();
18+
int correctAnswer;
19+
int numberOne = random.nextInt(100);
20+
int numberTwo = random.nextInt(100);
21+
int operationNumber = random.nextInt(3);
2422

2523
System.out.print("Question: " + numberOne);
2624

app/src/main/java/hexlet/code/games/Even.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package hexlet.code.games;
22

3+
import java.util.Random;
4+
35
public final class Even {
4-
private static int evenDivisor = 2;
5-
private static int oddRemainder = 1;
6-
private static StringBuilder correctAnswer = new StringBuilder();
76

87
private Even() {
98
throw new AssertionError("Utility class instantiation prohibited");
@@ -15,17 +14,20 @@ public static void startGameEven() {
1514

1615

1716
public static String generateCorrectAnswer() {
17+
Random random = new Random();
18+
StringBuilder correctAnswer = new StringBuilder();
19+
final int evenDivisor = 2;
20+
final int oddRemainder = 1;
1821
correctAnswer.setLength(0);
19-
int randomNumber = (int) (Math.random()*100);
22+
int randomNumber = random.nextInt(100);
23+
System.out.println("Question: " + randomNumber);
24+
2025
if(randomNumber % evenDivisor == oddRemainder) {
21-
correctAnswer.append("no");
26+
return "no";
2227
}
2328
else {
24-
correctAnswer.append("yes");
29+
return "yes";
2530
}
26-
27-
System.out.println("Question: " + randomNumber);
28-
return correctAnswer.toString();
2931
}
3032

3133

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package hexlet.code.games;
22

3+
import java.util.Random;
4+
35
public final class GCD {
4-
private static int numberA;
5-
private static int numberB;
6-
private static int numberC;
76

87
private GCD() {
98
throw new AssertionError("Utility class instantiation prohibited");
@@ -14,8 +13,10 @@ public static void startGameGCD() {
1413
}
1514

1615
public static String generateCorrectAnswer() {
17-
numberA = (int) (Math.random()*100);
18-
numberB = (int) (Math.random()*100);
16+
Random random = new Random();
17+
int numberA = random.nextInt(100);
18+
int numberB = random.nextInt(100);
19+
int numberC;
1920
System.out.println("Question: " + numberA + " " + numberB);
2021
while(true) {
2122
if(numberB > 0) {

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package hexlet.code.games;
22

3-
public final class Prime {
4-
private static int number;
5-
private static int maxValue;
6-
private final static int MIN_VALUE = 3;
7-
private final static int EVEN_DIVISOR = 2;
3+
import java.util.Random;
84

9-
10-
5+
public final class Prime {
116
private Prime() {
127
throw new AssertionError("Utility class instantiation prohibited");
138
}
@@ -17,8 +12,12 @@ public static void startGamePrime() {
1712
}
1813

1914
public static String generateCorrectAnswer() {
20-
number = (int) (Math.random()*100);
21-
maxValue = (int) (Math.ceil(Math.sqrt(number)));
15+
Random random = new Random();
16+
int number = random.nextInt(100);
17+
int maxValue = (int) (Math.ceil(Math.sqrt(number)));
18+
final int MIN_VALUE = 3;
19+
final int EVEN_DIVISOR = 2;
20+
2221
System.out.println("Question: " + number);
2322
if(number == EVEN_DIVISOR) {
2423
return "yes";

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package hexlet.code.games;
22

3+
import java.util.Random;
4+
5+
36
public final class Progression {
4-
private static int numberOne;
57
private static int numberTwo;
68
private static int sumOfNumbers;
79
private static int progressionLength;
8-
private static int maxValue = 20;
9-
private static int minValue = 7;
1010
private static int numberToSkip;
11-
private static StringBuilder progressionString = new StringBuilder();
12-
private static int correctAnswer;
1311

1412
private Progression() {
1513
throw new AssertionError("Utility class instantiation prohibited");
@@ -20,16 +18,20 @@ public static void startGameProgression() {
2018
}
2119

2220
public static String generateCorrectAnswer() {
23-
numberOne = (int) (Math.random()*100);
24-
numberTwo = (int) (Math.random()*100);
21+
Random random = new Random();
22+
int maxValue = 20;
23+
int minValue = 7;
24+
int numberOne = random.nextInt(100);
25+
numberTwo = random.nextInt(100);
2526
sumOfNumbers = numberOne;
26-
progressionLength = (int) (Math.random()*(maxValue - minValue + 1) + minValue);
27-
numberToSkip = (int) (Math.random()*progressionLength);
28-
correctAnswer = numberOne + numberTwo * numberToSkip;
27+
progressionLength = random.nextInt(maxValue - minValue + 1) + minValue;
28+
numberToSkip = random.nextInt(progressionLength);
29+
int correctAnswer = numberOne + numberTwo * numberToSkip;
2930
return String.valueOf(correctAnswer);
3031
}
3132

3233
public static void generateProgression() {
34+
StringBuilder progressionString = new StringBuilder();
3335
progressionString.setLength(0);
3436
progressionString.append("Question: ");
3537
for(int i = 0; i < progressionLength; i++) {

0 commit comments

Comments
 (0)