11package hexlet .code ;
22
3- import java .util .Arrays ;
4- import java .util .List ;
53import 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
78public 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