File tree Expand file tree Collapse file tree 3 files changed +32
-1
lines changed
app/src/main/java/hexlet/code Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 33import java .util .Scanner ;
44import hexlet .code .games .Even ;
55import hexlet .code .games .Calc ;
6+ import hexlet .code .games .GCD ;
67
78public class App {
89 public static void main (String [] args ) {
@@ -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 var userChoice = scanner .next ();
@@ -20,6 +22,7 @@ public static void main(String[] args) {
2022 case "1" -> Cli .greetUser (scanner );
2123 case "2" -> Even .play (scanner );
2224 case "3" -> Calc .play (scanner );
25+ case "4" -> GCD .play (scanner );
2326 default -> scanner .close ();
2427 }
2528 }
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ public static void play(Scanner scanner) {
99 System .out .println ("Answer 'yes' if the number is even, otherwise answer 'no'." );
1010
1111 while (Engine .isContinuing ()) {
12- var randomNumber = Engine .getRandomNumber (100 ) + 1 ; // added 1 to avoid 0 and keep equal chances
12+ var randomNumber = Engine .getRandomNumber (100 ) + 1 ; // added 1 to avoid 0
1313 var correctAnswer = randomNumber % 2 == 0 ? "yes" : "no" ;
1414
1515 System .out .println ("Question: " + randomNumber );
Original file line number Diff line number Diff line change 1+ package hexlet .code .games ;
2+
3+ import java .util .Scanner ;
4+
5+ public class GCD {
6+ public static int getGCD (int a , int b ) {
7+ return (a % b == 0 ) ? Math .abs (b ) : getGCD (b , a % b );
8+ }
9+
10+ public static void play (Scanner scanner ) {
11+ Engine .greet (scanner );
12+
13+ while (Engine .isContinuing ()) {
14+ var number1 = Engine .getRandomNumber (100 ) + 1 ; // added 1 to avoid 0
15+ var number2 = Engine .getRandomNumber (100 ) + 1 ;
16+ int correctAnswer = getGCD (number1 , number2 );
17+
18+ System .out .println ("Find the greatest common divisor of given numbers." );
19+ System .out .println ("Question: " + number1 + " " + number2 );
20+ System .out .print ("Your answer: " );
21+ var userAnswer = scanner .next ();
22+
23+ Engine .compareAnswers (userAnswer , Integer .toString (correctAnswer ));
24+ }
25+
26+ Engine .end (scanner );
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments