Skip to content

Commit 19c726d

Browse files
committed
added game №2 Even
1 parent 2f20de3 commit 19c726d

File tree

5 files changed

+226
-5
lines changed

5 files changed

+226
-5
lines changed

app/game2Even.cast

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{"version": 2, "width": 107, "height": 37, "timestamp": 1730980296, "env": {"SHELL": "/bin/zsh", "TERM": "xterm-256color"}}
2+
[0.040978, "o", "\u001b[1m\u001b[7m%\u001b[27m\u001b[1m\u001b[0m \r \r"]
3+
[0.046664, "o", "\r\u001b[0m\u001b[27m\u001b[24m\u001b[Jdaria@Air-Daria app main $ \u001b[K\u001b[?2004h"]
4+
[2.774348, "o", "\u001b[7mmake run-dist\u001b[27m"]
5+
[3.223886, "o", "\u001b[13D\u001b[27mm\u001b[27ma\u001b[27mk\u001b[27me\u001b[27m \u001b[27mr\u001b[27mu\u001b[27mn\u001b[27m-\u001b[27md\u001b[27mi\u001b[27ms\u001b[27mt\u001b[?2004l\r\r\n"]
6+
[3.245316, "o", "./build/install/app/bin/app\r\n"]
7+
[3.296848, "o", "Please enter the game number and press Enter.\r\n1 - Greet\r\n2 - Even\r\n0 - Exit\r\nYour choice:\r\n"]
8+
[5.420046, "o", "2"]
9+
[5.742638, "o", "\r\n"]
10+
[5.746123, "o", "Welcome to the Brain Games!\r\nMay I have your name?\r\n"]
11+
[6.749079, "o", "D"]
12+
[6.965792, "o", "a"]
13+
[8.062863, "o", "r"]
14+
[8.272257, "o", "i"]
15+
[8.378872, "o", "a"]
16+
[8.951936, "o", "\r\n"]
17+
[8.985723, "o", "Hello, Daria!\r\nAnswer 'yes' if the number is even, otherwise answer 'no'.\r\n"]
18+
[8.995565, "o", "Question: 50\r\nYour answer: \r\n"]
19+
[13.457539, "o", "y"]
20+
[14.202243, "o", "e"]
21+
[14.411826, "o", "s"]
22+
[14.526459, "o", "\r\n"]
23+
[14.526816, "o", "Correct!\r\n"]
24+
[14.52695, "o", "Question: 53\r\n"]
25+
[14.527064, "o", "Your answer: \r\n"]
26+
[18.306125, "o", "n"]
27+
[18.568058, "o", "o"]
28+
[19.166673, "o", "\r\n"]
29+
[19.167114, "o", "Correct!\r\n"]
30+
[19.167296, "o", "Question: 90\r\nYour answer: \r\n"]
31+
[22.90948, "o", "n"]
32+
[23.061664, "o", "o"]
33+
[23.385137, "o", "\r\n"]
34+
[23.386179, "o", "'no' is wrong answer ;(. Correct answer was 'yes'.\r\nLet's try again, Daria!\r\n"]
35+
[23.386325, "o", "Question: 89\r\nYour answer: \r\n"]
36+
[28.857393, "o", "n"]
37+
[29.147916, "o", "o"]
38+
[29.340576, "o", "\r\n"]
39+
[29.340928, "o", "Correct!\r\n"]
40+
[29.341041, "o", "Question: 67\r\nYour answer: \r\n"]
41+
[30.85621, "o", "n"]
42+
[31.347173, "o", "o"]
43+
[31.528241, "o", "\r\n"]
44+
[31.528742, "o", "Correct!\r\n"]
45+
[31.529129, "o", "Question: 2\r\nYour answer: \r\n"]
46+
[33.253827, "o", "y"]
47+
[33.81816, "o", "e"]
48+
[34.039223, "o", "s"]
49+
[34.280202, "o", "\r\n"]
50+
[34.280924, "o", "Correct!\r\n"]
51+
[34.281497, "o", "Congratulations, Daria!\r\n"]
52+
[34.288951, "o", "\u001b[1m\u001b[7m%\u001b[27m\u001b[1m\u001b[0m \r \r"]
53+
[34.307535, "o", "\r\u001b[0m\u001b[27m\u001b[24m\u001b[Jdaria@Air-Daria app main $ \u001b[K\u001b[?2004h"]
54+
[35.507617, "o", "e"]
55+
[35.76011, "o", "\bex"]
56+
[35.993834, "o", "i"]
57+
[36.311516, "o", "t"]
58+
[36.539296, "o", "\u001b[?2004l\r\r\n"]

app/gameEven.cast

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{"version": 2, "width": 107, "height": 37, "timestamp": 1730980575, "env": {"SHELL": "/bin/zsh", "TERM": "xterm-256color"}}
2+
[0.047584, "o", "\u001b[1m\u001b[7m%\u001b[27m\u001b[1m\u001b[0m \r \r"]
3+
[0.052784, "o", "\r\u001b[0m\u001b[27m\u001b[24m\u001b[Jdaria@Air-Daria app main $ \u001b[K\u001b[?2004h"]
4+
[0.856561, "o", "\u001b[7mmake run-dist\u001b[27m"]
5+
[1.697424, "o", "\u001b[13D\u001b[27mm\u001b[27ma\u001b[27mk\u001b[27me\u001b[27m \u001b[27mr\u001b[27mu\u001b[27mn\u001b[27m-\u001b[27md\u001b[27mi\u001b[27ms\u001b[27mt"]
6+
[1.697512, "o", "\u001b[?2004l\r\r\n"]
7+
[1.718357, "o", "./build/install/app/bin/app\r\n"]
8+
[1.769681, "o", "Please enter the game number and press Enter.\r\n1 - Greet\r\n2 - Even\r\n0 - Exit\r\nYour choice:\r\n"]
9+
[3.205585, "o", "2"]
10+
[3.668697, "o", "\r\n"]
11+
[3.671548, "o", "Welcome to the Brain Games!\r\nMay I have your name?\r\n"]
12+
[4.161989, "o", "D"]
13+
[4.351534, "o", "a"]
14+
[4.78109, "o", "r"]
15+
[5.034194, "o", "i"]
16+
[5.162248, "o", "a"]
17+
[5.817486, "o", "\r\n"]
18+
[5.828026, "o", "Hello, Daria!\r\n"]
19+
[5.82878, "o", "Answer 'yes' if the number is even, otherwise answer 'no'.\r\n"]
20+
[5.833068, "o", "Question: 2\r\nYour answer: \r\n"]
21+
[7.694179, "o", "y"]
22+
[7.952691, "o", "e"]
23+
[8.141885, "o", "s"]
24+
[8.391024, "o", "\r\n"]
25+
[8.391784, "o", "Correct!\r\n"]
26+
[8.392202, "o", "Question: 36\r\nYour answer: \r\n"]
27+
[9.825686, "o", "y"]
28+
[10.031345, "o", "e"]
29+
[10.231343, "o", "s"]
30+
[10.507382, "o", "\r\n"]
31+
[10.507858, "o", "Correct!\r\n"]
32+
[10.508148, "o", "Question: 62\r\nYour answer: \r\n"]
33+
[13.540041, "o", "y"]
34+
[13.644143, "o", "e"]
35+
[13.830632, "o", "s"]
36+
[13.997881, "o", "\r\n"]
37+
[13.99846, "o", "Correct!\r\n"]
38+
[13.999036, "o", "Congratulations, Daria!\r\n"]
39+
[14.006092, "o", "\u001b[1m\u001b[7m%\u001b[27m\u001b[1m\u001b[0m \r \r"]
40+
[14.02349, "o", "\r\u001b[0m\u001b[27m\u001b[24m\u001b[Jdaria@Air-Daria app main $ \u001b[K\u001b[?2004h"]
41+
[15.755628, "o", "e"]
42+
[15.98186, "o", "\bex"]
43+
[16.293766, "o", "i"]
44+
[16.607217, "o", "t"]
45+
[16.790921, "o", "\u001b[?2004l\r\r\n"]
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
package hexlet.code;
22

3+
import java.util.Scanner;
4+
35
public class App {
46
public static void main(String[] args) {
5-
Cli.sayHello();
7+
Scanner scanner = new Scanner(System.in);
8+
9+
System.out.println("""
10+
Please enter the game number and press Enter.
11+
1 - Greet
12+
2 - Even
13+
0 - Exit
14+
Your choice:""");
15+
16+
String choice = scanner.nextLine();
17+
18+
switch (choice) {
19+
case "1" ->
20+
Cli.sayHello();
21+
22+
case "0" ->
23+
Cli.sayGoodbye();
24+
25+
case "2" ->
26+
Games.even();
27+
28+
default -> System.out.println("Invalid choice, please try again.");
29+
30+
}
31+
scanner.close();
632
}
733
}

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,59 @@
33
import java.util.Scanner;
44

55
public class Cli {
6-
public static void sayHello() {
6+
public static String sayHello() {
7+
78
Scanner scanner = new Scanner(System.in);
89

9-
System.out.println("Welcome to the Brain Games!");
10-
System.out.print("May I have your name? ");
10+
System.out.println("""
11+
Welcome to the Brain Games!
12+
May I have your name?""");
1113
String name = scanner.nextLine();
1214

1315
System.out.println("Hello, " + name + "!");
1416

15-
scanner.close();
17+
return name;
18+
}
19+
20+
public static void sayGoodbye() {
21+
System.out.println("Bye!");
1622
}
1723
}
24+
25+
26+
27+
28+
29+
30+
31+
//Cli.sayHello();
32+
//Scanner scanner = new Scanner(System.in);
33+
//Random random = new Random();
34+
//int randomNumber = random.nextInt(100);
35+
// System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
36+
// System.out.println("Question: " + randomNumber);
37+
// System.out.println("Your answer: ");
38+
//String answer = scanner.nextLine();
39+
//int count = 0;
40+
// while (count < 4) {
41+
// if (randomNumber % 2 == 0) {
42+
//var rightAnswer1 = "yes";
43+
// if (answer.equals(rightAnswer1)) {
44+
// System.out.println("Correct!");
45+
// return count = count + 1;
46+
// }
47+
// System.out.println("'no' is wrong answer ;(. Correct answer was 'yes'.\n" +
48+
// "Let's try again, " + name + "!");
49+
//
50+
// } else if (randomNumber % 2 != 0) {
51+
//var rightAnswer2 = "no";
52+
// if (answer.equals(rightAnswer2)) {
53+
// System.out.println("Correct!");
54+
// return count = count + 1;
55+
// }
56+
// System.out.println("'yes' is wrong answer ;(. Correct answer was 'no'.\n" +
57+
// "Let's try again, " + name + "!");
58+
// }
59+
//
60+
// }
61+
// return randomNumber;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package hexlet.code;
2+
3+
import java.util.Scanner;
4+
import java.util.Random;
5+
6+
public class Games {
7+
8+
// Game №2 Even
9+
public static int even() {
10+
String name = Cli.sayHello();
11+
12+
Scanner scanner = new Scanner(System.in);
13+
Random random = new Random();
14+
15+
System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
16+
17+
int count = 0;
18+
19+
while (count < 3) {
20+
int randomNumber = random.nextInt(99) + 1;
21+
System.out.println("Question: " + randomNumber);
22+
System.out.println("Your answer: ");
23+
String answer = scanner.nextLine();
24+
25+
if ((randomNumber % 2 == 0 && answer.equals("yes"))
26+
|| (randomNumber % 2 != 0 && answer.equals("no"))) {
27+
System.out.println("Correct!");
28+
count++;
29+
30+
} else {
31+
if (randomNumber % 2 == 0) {
32+
System.out.println("'no' is wrong answer ;(. Correct answer was 'yes'.\n"
33+
+ "Let's try again, "
34+
+ name
35+
+ "!");
36+
} else {
37+
System.out.println("'yes' is wrong answer ;(. Correct answer was 'no'.\n"
38+
+ "Let's try again, "
39+
+ name
40+
+ "!");
41+
}
42+
count = 0;
43+
}
44+
}
45+
System.out.println("Congratulations, " + name + "!");
46+
return count;
47+
}
48+
}

0 commit comments

Comments
 (0)