Explorar el Código

Changed how the program logs 'consecutive guesses' in new copy

Jennifer Chao hace 6 años
padre
commit
591f469708

BIN
.DS_Store Ver fichero


BIN
TooLargeTooSmall/CopyOfMain.class Ver fichero


+ 5
- 0
TooLargeTooSmall/CopyOfMain.ctxt Ver fichero

@@ -0,0 +1,5 @@
1
+#BlueJ class context
2
+comment0.target=CopyOfMain
3
+comment1.params=args
4
+comment1.target=void\ main(java.lang.String[])
5
+numComments=2

+ 59
- 0
TooLargeTooSmall/CopyOfMain.java Ver fichero

@@ -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
+}

+ 16
- 9
TooLargeTooSmall/package.bluej Ver fichero

@@ -1,20 +1,20 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=0
3
-editor.fx.0.width=0
4
-editor.fx.0.x=0
5
-editor.fx.0.y=0
2
+editor.fx.0.height=722
3
+editor.fx.0.width=800
4
+editor.fx.0.x=223
5
+editor.fx.0.y=151
6 6
 objectbench.height=101
7 7
 objectbench.width=776
8 8
 package.divider.horizontal=0.6
9 9
 package.divider.vertical=0.8007380073800738
10 10
 package.editor.height=427
11 11
 package.editor.width=674
12
-package.editor.x=426
13
-package.editor.y=53
12
+package.editor.x=773
13
+package.editor.y=245
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0
17
-package.numTargets=1
17
+package.numTargets=2
18 18
 package.showExtends=true
19 19
 package.showUses=true
20 20
 project.charset=UTF-8
@@ -28,5 +28,12 @@ target1.name=Main
28 28
 target1.showInterface=false
29 29
 target1.type=ClassTarget
30 30
 target1.width=80
31
-target1.x=150
32
-target1.y=60
31
+target1.x=190
32
+target1.y=160
33
+target2.height=50
34
+target2.name=CopyOfMain
35
+target2.showInterface=false
36
+target2.type=ClassTarget
37
+target2.width=100
38
+target2.x=400
39
+target2.y=170