forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessMyNumber.java
More file actions
29 lines (23 loc) · 766 Bytes
/
Copy pathGuessMyNumber.java
File metadata and controls
29 lines (23 loc) · 766 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
//Exercise 3-4.
import java.util.Random;
import java.util.Scanner;
public class GuessMyNumber
{
public static void main(String[] args)
{
// pick a random number
Random random = new Random();
int number = random.nextInt(100) + 1;
// printing this so I can check the code
System.out.println(number);
// prompt the user and get the value
int guess;
Scanner in = new Scanner(System.in);
System.out.print("Choose a number: ");
guess = in.nextInt();
int difference = number - guess;
System.out.println("You chose: " + guess);
System.out.println("The number I chose was: " + number);
System.out.println("You were off by: " + difference);
}
}