File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed
Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change 11package tech .gladiator ;
22
3+ import java .util .Random ;
4+ import java .util .Scanner ;
5+
36public class Main {
47
58 public static void main (String [] args ) {
6- // write your code here
9+ Random random = new Random ();
10+
11+ int number = random .nextInt (100 ) + 1 ;
12+
13+ System .out .printf ("Welcome to the game!%n%nPlease enter a number between 1 and 100%n%n" );
14+
15+ int tries = 5 ;
16+
17+ while (tries > 0 ) {
18+ Scanner scanner = new Scanner (System .in );
19+
20+ int guess = scanner .nextInt ();
21+
22+ tries -= 1 ;
23+ if (guess == number ) {
24+ System .out .printf ("You guessed correctly!%n%n" );
25+ break ;
26+ } else if (guess < number ) {
27+ System .out .printf ("Your guess was too low. Please try again. You have %d tries left.%n%n" , tries );
28+ } else {
29+ System .out .printf ("Your guess was too high. Please try again. You have %d tries left.%n%n" , tries );
30+ }
31+ }
32+
33+ System .out .printf ("The number was %d." , number );
734 }
835}
You can’t perform that action at this time.
0 commit comments