Skip to content

Commit 2cbf454

Browse files
committed
Ex 5.2 is done
1 parent 3ce71bc commit 2cbf454

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎ch05/Ex_5_2.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Random;
2+
import java.util.Scanner;
3+
4+
public class Ex_5_2 {
5+
public static void main(String[] args) {
6+
Random random = new Random();
7+
int number = random.nextInt(100) + 1;
8+
int userGuess;
9+
int userAttempts = 3;
10+
11+
System.out.println("I'm thinking of a number between 1 and 100");
12+
System.out.println("(including both). Can you guess what it is?");
13+
14+
while(userAttempts != 0 ) {
15+
Scanner scanner = new Scanner(System.in);
16+
System.out.print("Type a number ");
17+
userGuess = scanner.nextInt();
18+
19+
if (userGuess < number) {
20+
System.out.println("Your number is less than the number I'm thinking about");
21+
userAttempts--;
22+
} else if (userGuess > number) {
23+
System.out.println("Your number is bigger than the number I'm thinking about");
24+
userAttempts--;
25+
} else {
26+
System.out.println("Congratulations! You guessed right!");
27+
}
28+
29+
if (userAttempts == 0) {
30+
System.out.println("I'm sorry you are out of attempts");
31+
System.out.println("The number I was thinking of is: " + number);
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)