forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessNumber.java
More file actions
27 lines (20 loc) · 767 Bytes
/
Copy pathGuessNumber.java
File metadata and controls
27 lines (20 loc) · 767 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
import java.util.Scanner;
import java.util.Random;
public class GuessNumber
{
public static void main(String[] args)
{
int userGuess;
Scanner in = new Scanner (System.in);
//prompt user to guess a number
System.out.print("Guess a number between 1 and 100: ");
userGuess = in.nextInt();
// pick a random number
Random random = new Random();
int number = random.nextInt(100) + 1;
int difference = Math.abs(number - userGuess);
System.out.println("Your guess was " + userGuess + ".");
System.out.println("The number I was thinking of was " + number + ".");
System.out.println("The difference between your guess and my number is " + difference + ".");
}
}