Skip to content

Commit 76236fd

Browse files
add Gcd.java, mod App.java, Calc.java, Even.java
1 parent e90710f commit 76236fd

File tree

6 files changed

+106
-4
lines changed

6 files changed

+106
-4
lines changed

app/.idea/gradle.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.idea/workspace.xml

Lines changed: 52 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import hexlet.code.games.Calc;
44
import hexlet.code.games.Even;
5+
import hexlet.code.games.Gcd;
56

67
import java.util.Scanner;
78

@@ -12,6 +13,7 @@ public static void main(String[] args) {
1213
System.out.println("1 - Greet");
1314
System.out.println("2 - Even");
1415
System.out.println("3 - Calc");
16+
System.out.println("4 - GCD");
1517
System.out.println("0 - Exit");
1618
System.out.print("Your choice : ");
1719
String option = scanner.nextLine();
@@ -26,6 +28,9 @@ public static void main(String[] args) {
2628
case "3":
2729
Calc.play();
2830
break;
31+
case "4":
32+
Gcd.play();
33+
break;
2934
}
3035
scanner.close();
3136
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void play() {
3636
if (answer == rightAnswer) {
3737
System.out.println("Correct!");
3838
} else {
39-
System.out.println("'" + answer + "' is wrong answer ;(. Correct answer was '" + rightAnswer + "'." );
39+
System.out.println("'" + answer + "' is wrong answer ;(. Correct answer was '" + rightAnswer + "'.");
4040
System.out.println("Let's try again, " + userName + "!");
4141
return;
4242
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public static void play() {
2323
if (answer.equals(rightAnswer)) {
2424
System.out.println("Correct!");
2525
} else {
26-
System.out.println("'" + answer + "'" + " is wrong answer ;(. Correct answer was " + "'" + rightAnswer + "'.\nLet's try again, " + userName);
26+
System.out.println("'" + answer + "'" + " is wrong answer ;(. Correct answer was " + "'" + rightAnswer + "'.");
27+
System.out.println("Let's try again, " + userName + "!");
2728
return;
2829
}
2930
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package hexlet.code.games;
2+
3+
import hexlet.code.Engine;
4+
5+
import java.util.Scanner;
6+
7+
public class Gcd {
8+
public static void play() {
9+
String userName = Engine.description();
10+
System.out.println("Find the greatest common divisor of given numbers.");
11+
Scanner scanner = new Scanner(System.in);
12+
13+
for (int i = 1; i <= Engine.rounds; i++) {
14+
int firstValue = (int) (Math.random() * 100) + 1;
15+
int secondValue = (int) (Math.random() * 100) + 1;
16+
int rightAnswer = 0;
17+
18+
int tempFirstValue = firstValue;
19+
int tempSecondValue = secondValue;
20+
21+
while (tempSecondValue != 0) {
22+
rightAnswer = tempSecondValue;
23+
tempSecondValue = tempFirstValue % tempSecondValue;
24+
tempFirstValue = rightAnswer;
25+
}
26+
27+
System.out.println("Question: " + firstValue + " " + secondValue);
28+
System.out.print("Your answer: ");
29+
int answer = scanner.nextInt();
30+
31+
if (answer == rightAnswer) {
32+
System.out.println("Correct!");
33+
} else {
34+
System.out.println("'" + answer + "' is wrong answer ;(. Correct answer was '" + rightAnswer + "'");
35+
System.out.println("Let's try again, " + userName + "!");
36+
return;
37+
}
38+
}
39+
System.out.println("Congratulations, " + userName + "!");
40+
}
41+
}

0 commit comments

Comments
 (0)