File tree Expand file tree Collapse file tree 3 files changed +58
-5
lines changed
app/src/main/java/hexlet/code Expand file tree Collapse file tree 3 files changed +58
-5
lines changed Original file line number Diff line number Diff line change 11package hexlet .code ;
22
3+ import java .util .Scanner ;
4+
35public 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}
Original file line number Diff line number Diff line change 33import java .util .Scanner ;
44
55public 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments