Skip to content

Commit cd314fe

Browse files
committed
Add asciinema to README, edit App and Cli, add Even
1 parent 2101b81 commit cd314fe

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
package hexlet.code;
22

3+
import java.util.Scanner;
34

45
public class App {
56
public static void main(String[] args) {
6-
System.out.println("Welcome to the Brain Games!");
7-
Cli.greetUser();
7+
System.out.println("Please enter the game number and press Enter.");
8+
9+
Scanner scanner = new Scanner(System.in);
10+
System.out.println("1 - Greet");
11+
System.out.println("2 - Even");
12+
System.out.println("0 - Exit");
13+
String digit = scanner.next();
14+
System.out.println("Your choice: " + digit);
15+
16+
if (digit.equals("1")) {
17+
System.out.println("Welcome to the Brain Games!");
18+
Cli.greetUser();
19+
} else if (digit.equals("0")) {
20+
System.out.println("Goodbye!");
21+
} else if (digit.equals("2")) {
22+
System.out.println("Welcome to the Brain Games!");
23+
String userName = Cli.greetUser();
24+
Even.generateNumber(userName);
25+
} else {
26+
System.out.println("Invalid choice");
27+
}
28+
scanner.close();
829
}
930
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import java.util.Scanner;
33

44
public class Cli {
5-
public static void greetUser() {
5+
public static String greetUser() {
66
Scanner scanner = new Scanner(System.in);
77
System.out.print("May i have your name? ");
88
String userName = scanner.next();
99
System.out.println("Hello, " + userName + "!");
10+
return userName;
1011

11-
scanner.close();
1212
}
1313
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package hexlet.code;
2+
import java.util.Scanner;
3+
4+
public class Even {
5+
public static boolean generateNumber(String userName) {
6+
Scanner scanner = new Scanner(System.in);
7+
System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'");
8+
int correctAnswerCount = 0;
9+
final int rounds = 3;
10+
while (correctAnswerCount <= rounds) {
11+
int random = (int) (Math.random() * 100);
12+
System.out.println("Question: " + random);
13+
String answer = scanner.next();
14+
System.out.println("Your answer: " + answer);
15+
boolean isCorrect = random % 2 == 0 ? answer.equals("yes") : answer.equals("no");
16+
if (isCorrect) {
17+
correctAnswerCount++;
18+
System.out.println("Correct!");
19+
if (correctAnswerCount == rounds) {
20+
System.out.println("Congratulations, " + userName + "!");
21+
return true;
22+
}
23+
} else {
24+
String correctAnswer = random % 2 == 0 ? "'yes'" : "'no'";
25+
System.out.println("'" + answer + "' is wrong answer ;(. Correct answer was " + correctAnswer
26+
+ ".\nLet's try again, " + userName + "!");
27+
return false;
28+
}
29+
}
30+
return true;
31+
}
32+
}
33+

0 commit comments

Comments
 (0)