Skip to content

Commit d64b99c

Browse files
committed
added game №4 gcd
1 parent 941c096 commit d64b99c

File tree

3 files changed

+34
-60
lines changed

3 files changed

+34
-60
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static void main(String[] args) {
1111
1 - Greet
1212
2 - Even
1313
3 - Calc
14+
4 - GCD
1415
0 - Exit
1516
Your choice:""");
1617

@@ -25,6 +26,8 @@ public static void main(String[] args) {
2526

2627
case "3" -> Games.calc();
2728

29+
case "4" -> Games.gcd();
30+
2831
default -> System.out.println("Invalid choice, please try again.");
2932

3033
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static void skeletonOfGames(String mainGameQuestion, String[] question, S
2525

2626
} else {
2727
System.out.println("'" + userAnswer + "'" + "is wrong answer ;(. "
28-
+ "Correct answer was " + "'" + correctAnswer[i] + "'" + "."
28+
+ "Correct answer was " + "'" + correctAnswer[i] + "'" + ". \n"
2929
+ "Let's try again, "
3030
+ name
3131
+ "!");
Lines changed: 30 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package hexlet.code;
22

3-
import java.util.Arrays;
4-
import java.util.List;
53
import java.util.Random;
4+
import java.util.Arrays; // for game №3 calc
5+
import java.util.List; // for game №3 calc
6+
import java.math.BigInteger; // for game №4 gcd
67

78
public class Games {
89

@@ -21,7 +22,6 @@ public static int even() {
2122
correctAnswer[i] = (randomNumber % 2 == 0) ? "yes" : "no";
2223
}
2324
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
24-
2525
return 0;
2626
}
2727

@@ -61,62 +61,33 @@ public static int calc() {
6161
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
6262
return 0;
6363
}
64-
}
6564

6665

66+
// Game №4 GCD
67+
public static int gcd() {
68+
Random random = new Random();
69+
String mainGameQuestion = "Find the greatest common divisor of given numbers.";
70+
71+
String[] question = new String[Engine.rounds];
72+
String[] correctAnswer = new String[Engine.rounds];
73+
74+
for (var i = 0; i < Engine.rounds; i++) {
75+
76+
int randomCommon = random.nextInt(10) + 2;
77+
int randomNumber1 = randomCommon * (random.nextInt(10) + 1);
78+
int randomNumber2 = randomCommon * (random.nextInt(10) + 1);
79+
80+
BigInteger bigA = BigInteger.valueOf(randomNumber1);
81+
BigInteger bigB = BigInteger.valueOf(randomNumber2);
82+
83+
String gcd = String.valueOf((bigA).gcd(bigB));
84+
85+
question[i] = randomNumber1 + " " + randomNumber2;
86+
correctAnswer[i] = gcd;
87+
}
88+
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
89+
return 0;
90+
}
91+
}
92+
6793

68-
// while (count < rounds) {
69-
// int randomNumber1 = random.nextInt(19) + 1;
70-
// int randomNumber2 = random.nextInt(9) + 1;
71-
//
72-
// String plus = randomNumber1 + "+" + randomNumber2;
73-
// String minus = randomNumber1 + "-" + randomNumber2;
74-
// String mult = randomNumber1 + "*" + randomNumber2;
75-
//
76-
// int resultPlus = randomNumber1 + randomNumber2;
77-
// int resultMinus = randomNumber1 - randomNumber2;
78-
// int resultMult = randomNumber1 * randomNumber2;
79-
//
80-
// List<String> givenList = Arrays.asList(plus, minus, mult);
81-
// Random rand = new Random();
82-
// String randomQuestion = givenList.get(rand.nextInt(givenList.size()));
83-
//
84-
// System.out.println("Question: " + randomQuestion);
85-
// System.out.println("Your answer: ");
86-
// String answer = scanner.nextLine();
87-
//
88-
// if (randomQuestion.equals(plus) && Integer.parseInt(answer) == resultPlus
89-
// || randomQuestion.equals(minus) && Integer.parseInt(answer) == resultMinus
90-
// || randomQuestion.equals(mult) && Integer.parseInt(answer) == resultMult) {
91-
// System.out.println("Correct!");
92-
// count++;
93-
//
94-
// } else {
95-
// if (randomQuestion.equals(plus)) {
96-
// System.out.println("'" + answer + "'" + " is wrong answer ;(. " +
97-
// "Correct answer was " + "'" + resultPlus + "'" + ".\n"
98-
// + "Let's try again, "
99-
// + name
100-
// + "!");
101-
// } else if (randomQuestion.equals(minus)){
102-
// System.out.println("'" + answer + "'" + " is wrong answer ;(. " +
103-
// "Correct answer was " + "'" + resultMinus + "'" + ".\n"
104-
// + "Let's try again, "
105-
// + name
106-
// + "!");
107-
// } else {
108-
// System.out.println("'" + answer + "'" + " is wrong answer ;(. " +
109-
// "Correct answer was " + "'" + resultMult + "'" + ".\n"
110-
// + "Let's try again, "
111-
// + name
112-
// + "!");
113-
// }
114-
// count = 0;
115-
// return count;
116-
// }
117-
// }
118-
// System.out.println("Congratulations, " + name + "!");
119-
// return count;
120-
// }
121-
//}
122-
//

0 commit comments

Comments
 (0)