forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_5_2.java
More file actions
38 lines (32 loc) · 1.45 KB
/
Exercise_5_2.java
File metadata and controls
38 lines (32 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;
import java.util.Random;
public class Exercise_5_2 {
public static void main(String[] args) {
// importing the random module
Random random = new Random();
int number = random.nextInt(100) + 1;
// System.out.println(number);
// importing the ability to get user input
Scanner in = new Scanner(System.in);
// establish the user
int userGuesses = 0;
// System.out.println(userGuesses);
System.out.println("I'm thinking of a number between 1 and 100.\n You have three tries. Can you guess what it is?");
System.out.print("Type a number: ");
//While loop for allowing poeple to guess the number
while (userGuesses < 3) {
int userInput = in.nextInt();
if (userInput != number) {
userGuesses += 1;
System.out.println("Your guess was incorrect");
int calc = (number - userInput);
System.out.printf("You were off by: %d\n", calc);
System.out.print("Try again: ");
} else {
System.out.printf("You guessed the correct number, congrats!\n");
System.out.printf("The number I was thinking of is: %d\n", number);
break;
}
}
}
}