Skip to content

Commit 84203e7

Browse files
committed
Added EvenGame class and some minor changes
1 parent 5423fe7 commit 84203e7

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
package hexlet.code;
22

3+
import java.util.Scanner;
4+
35
public class App {
46
public static void main(String[] args) {
5-
System.out.println("Welcome to the Brain Games!");
6-
Cli.greeting();
7+
System.out.println("""
8+
Please enter the game number and press Enter.
9+
1 - Greet
10+
2 - Even
11+
0 - Exit""");
12+
int gameChoice;
13+
Scanner input = new Scanner(System.in);
14+
gameChoice = input.nextInt();
15+
System.out.println("Your choice: " + gameChoice);
16+
switch (gameChoice) {
17+
case 1:
18+
Cli.greeting();
19+
break;
20+
case 2:
21+
EvenGame.evenGame();
22+
break;
23+
}
724
}
825
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
import java.util.Scanner;
44

55
public class Cli {
6+
public static String name;
7+
68
public static void greeting() {
7-
System.out.print("May I have your name? ");
8-
Scanner input = new Scanner(System.in);
9-
String name = input.nextLine();
9+
System.out.println("Welcome to the Brain Games!");
10+
if(name == null){
11+
System.out.print("May I have your name? ");
12+
Scanner input = new Scanner(System.in);
13+
name = input.nextLine();
14+
}
1015
System.out.println("Hello, " + name + "!");
16+
1117
}
1218
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package hexlet.code;
2+
3+
import java.util.Scanner;
4+
5+
public class EvenGame {
6+
7+
public static void evenGame() {
8+
Cli.greeting();
9+
System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
10+
int gameCount = 0;
11+
Scanner input = new Scanner(System.in);
12+
while (gameCount < 3) {
13+
int guessedNumber = (int) (Math.random() * 100.0);
14+
String trueAnswer;
15+
trueAnswer = guessedNumber % 2 == 0 ? "yes" : "no";
16+
System.out.println("Question: " + guessedNumber);
17+
String playerAnswer;
18+
playerAnswer = input.nextLine();
19+
if (trueAnswer.equals(playerAnswer)) {
20+
System.out.println("Correct!");
21+
gameCount++;
22+
} else {
23+
System.out.println("'" + playerAnswer + "' is wrong answer ;(. Correct answer was '" + trueAnswer + "'.");
24+
System.out.println("Let's try again, " + Cli.name + "!");
25+
gameCount = 0;
26+
}
27+
}
28+
System.out.println("Congratulations, " + Cli.name + "!");
29+
}
30+
}

0 commit comments

Comments
 (0)