Skip to content

Commit 3c31336

Browse files
author
Aleksandr Pronichev
committed
add new game
1 parent 86beceb commit 3c31336

File tree

1 file changed

+31
-0
lines changed
  • app/src/main/java/hexlet/code/games

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package hexlet.code.games;
2+
3+
import hexlet.code.Engine;
4+
5+
public class GCD {
6+
7+
private static final String RULES = "Find the greatest common divisor of given numbers.";
8+
9+
public static void gameGCD() {
10+
String[] questions = new String[Engine.ROUNDS];
11+
String[] correctAnswers = new String[Engine.ROUNDS];
12+
13+
for (int i = 0; i < Engine.ROUNDS; i++) {
14+
int firstNumber = (int) (Math.random() * 100);
15+
int secondNumber = (int) (Math.random() * 100);
16+
questions[i] = firstNumber + " " + secondNumber;
17+
int gcd = calculateGCD(firstNumber, secondNumber);
18+
correctAnswers[i] = Integer.toString(gcd);
19+
}
20+
Engine.game(RULES, questions, correctAnswers);
21+
}
22+
23+
public static int calculateGCD(int a, int b) {
24+
while (b != 0) {
25+
int tmp = b;
26+
b = a % b;
27+
a = tmp;
28+
}
29+
return a;
30+
}
31+
}

0 commit comments

Comments
 (0)