File tree Expand file tree Collapse file tree 3 files changed +58
-4
lines changed
app/src/main/java/hexlet/code Expand file tree Collapse file tree 3 files changed +58
-4
lines changed Original file line number Diff line number Diff line change 11package hexlet .code ;
22
3+ import java .util .Scanner ;
34
45public 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}
Original file line number Diff line number Diff line change 22import java .util .Scanner ;
33
44public 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}
Original file line number Diff line number Diff line change 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+ + ".\n Let's try again, " + userName + "!" );
27+ return false ;
28+ }
29+ }
30+ return true ;
31+ }
32+ }
33+
You can’t perform that action at this time.
0 commit comments