File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Random ;
2+ import java .util .Scanner ;
3+
4+ public class Main {
5+ public static void main (String [] args ){
6+
7+ // NUMBER GUESSING GAME
8+
9+ Random random = new Random ();
10+ Scanner scanner = new Scanner (System .in );
11+
12+ // Declare variables
13+ int guess ;
14+ int attempts = 0 ;
15+ int min = 1 ;
16+ int max = 100 ;
17+ int randomNumber = random .nextInt (min , max + 1 );
18+
19+ /*
20+ Guess the number in a certain range (min-max range). If the guess is right, you win. Else, try again!
21+ */
22+ System .out .println ("..........NUMBER GUESSING GAME.........." );
23+ System .out .printf ("Guess a number between %d-%d:\n " , min , max );
24+
25+ do {
26+ System .out .print ("Enter a guess: " );
27+ guess = scanner .nextInt ();
28+ attempts ++;
29+
30+ if (guess < randomNumber ){
31+ System .out .println ("TOO LOW! Try again!" );
32+ }
33+ else if (guess > randomNumber ){
34+ System .out .println ("TOO HIGH! Try again!" );
35+ }
36+ else {
37+ System .out .println ("CORRECT! The number was " + randomNumber );
38+ System .out .println ("Number of attempts: " + attempts );
39+ }
40+
41+ }while (guess != randomNumber );
42+
43+ System .out .println ("You have won!" );
44+
45+ scanner .close ();
46+
47+ }
48+ }
You can’t perform that action at this time.
0 commit comments