forked from ChrisMayfield/ThinkJava2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessSoln.java
More file actions
36 lines (29 loc) · 1.05 KB
/
Copy pathGuessSoln.java
File metadata and controls
36 lines (29 loc) · 1.05 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
import java.util.Scanner;
import java.util.Random;
/**
* Solution code for the "guess my number" exercise.
*
* @author Allen Downey
* @version 6.5.0
*/
public class GuessSoln {
public static void main(String[] args) {
// create a scanner
Scanner in = new Scanner(System.in);
// pick a random number
Random random = new Random();
int number = random.nextInt(100) + 1;
// display the prompt
System.out.println("I'm thinking of a number between 1 and 100");
System.out.println("(including both). Can you guess what it is?");
System.out.print("Type a number: ");
// parse input from the user
String line = in.nextLine();
int guess = Integer.parseInt(line);
// display the results
System.out.println("Your guess is: " + guess);
System.out.println("The number I was thinking of is: " + number);
System.out.println("You were off by: " + (guess - number));
// note: what happens if you don't have parens around guess - number?
}
}