Skip to content

Commit 3305f76

Browse files
authored
Update GuessStarter.java
Completed starter code with complete solution.
1 parent 6b758bd commit 3305f76

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

ch03/GuessStarter.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import java.util.Random;
2+
import java.util.Scanner;
23

3-
/**
4-
* Starter code for the "Guess My Number" exercise.
5-
*/
64
public class GuessStarter {
7-
85
public static void main(String[] args) {
6+
// introduce the guessing game
7+
System.out.println("I'm thinking of a number between 1 and 100 (including both). " +
8+
"Can you guess what it is? ");
9+
10+
// ask for input
11+
Scanner in = new Scanner(System.in);
12+
System.out.print("Type a number: ");
13+
int guess = in.nextInt();
14+
System.out.println("Your Guess is: " + guess);
15+
916
// pick a random number
1017
Random random = new Random();
1118
int number = random.nextInt(100) + 1;
12-
System.out.println(number);
19+
System.out.println("The number I was thinking of is: " + number);
20+
int diff = Math.abs(guess - number);
21+
System.out.println("You were off by: " + diff);
22+
1323
}
1424

1525
}
26+

0 commit comments

Comments
 (0)