forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessStarter.java
More file actions
33 lines (24 loc) · 978 Bytes
/
GuessStarter.java
File metadata and controls
33 lines (24 loc) · 978 Bytes
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
import java.util.Random;
import java.util.Scanner;
import static java.lang.Math.abs;
/**
* "Guess My Number" exercise.
*/
public class GuessStarter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("I'm thinking of a number between 1 and 100");
System.out.println("(including both). Can you guess what it is?");
//prompt the user and get the value
System.out.print("Type a number: ");
int guess = in.nextInt();
System.out.printf("Your guess is: %d\n", guess);
// pick a random number
Random random = new Random();
int number = random.nextInt(100) + 1;
System.out.print("The number I was thinking of is: ");
System.out.println(number);
//compute and display the difference between the user's guess and the number that was generated
System.out.printf("You were off by: %d", abs(number - guess));
}
}