|
@@ -0,0 +1,59 @@
|
|
1
|
+import java.util.Scanner;
|
|
2
|
+import java.util.Random;
|
|
3
|
+
|
|
4
|
+public class CopyOfMain {
|
|
5
|
+
|
|
6
|
+ public static void main(String[] args) {
|
|
7
|
+
|
|
8
|
+ // Create random number from range of 0 to 100.
|
|
9
|
+ Random rand = new Random();
|
|
10
|
+ int mysteryNum = rand.nextInt(101);
|
|
11
|
+
|
|
12
|
+ // Log number of user inputs.
|
|
13
|
+ int userAttempts = 1;
|
|
14
|
+
|
|
15
|
+ boolean[] alreadyGuess = new boolean[101];
|
|
16
|
+
|
|
17
|
+ int userGuess;
|
|
18
|
+ // int prevGuess = 101;
|
|
19
|
+
|
|
20
|
+ // Continue looping infinitely until correct answer is passed. Count each attempt.
|
|
21
|
+ do {
|
|
22
|
+ for (; ; userAttempts++) {
|
|
23
|
+ Scanner input = new Scanner(System.in);
|
|
24
|
+
|
|
25
|
+ System.out.println("What positive integer between 0 and 100 am I thinking of?");
|
|
26
|
+ userGuess = input.nextInt();
|
|
27
|
+
|
|
28
|
+ System.out.println("You guessed: " + userGuess);
|
|
29
|
+
|
|
30
|
+ // Check what the mystery number is. (personal use for testing)
|
|
31
|
+ // System.out.println(mysteryNum);
|
|
32
|
+
|
|
33
|
+ // Empty variable for storing the previous guess to compare.
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+ // Conditionals (correct, too high, too low, same guess, out of bounds)
|
|
37
|
+
|
|
38
|
+ if (mysteryNum == userGuess) {
|
|
39
|
+ System.out.println("Correct! Number of attempts: " + userAttempts);
|
|
40
|
+ break;
|
|
41
|
+ } else if (userGuess > 100 || userGuess < 0) {
|
|
42
|
+ System.out.println("Is that number between 0 and 100?");
|
|
43
|
+ } else if (alreadyGuess[userGuess] == true) {
|
|
44
|
+ System.out.println("You already guessed that number!");
|
|
45
|
+ userAttempts -= 1;
|
|
46
|
+ } else if (mysteryNum > userGuess) {
|
|
47
|
+ System.out.println("Too low! Try again!");
|
|
48
|
+ } else if (mysteryNum < userGuess) {
|
|
49
|
+ System.out.println("Too high! Try again!");
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ // Save previous guess for comparison.
|
|
53
|
+ // prevGuess = userGuess;
|
|
54
|
+ alreadyGuess[userGuess] = true;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ } while (mysteryNum != userGuess);
|
|
58
|
+ }
|
|
59
|
+}
|