Browse Source

FINISHED TooLargeTooSmall ADDED FUNCTIONALITY

Nathan Hall 6 years ago
parent
commit
7fd07b9ce3

BIN
TooLargeTooSmall/GuessingGame.class View File


+ 50
- 21
TooLargeTooSmall/GuessingGame.java View File

10
 import java.util.Random;
10
 import java.util.Random;
11
 
11
 
12
 public class GuessingGame{
12
 public class GuessingGame{
13
-    
13
+
14
     public static void main(String[] args) {
14
     public static void main(String[] args) {
15
-    
15
+
16
         System.out.println("GUESSING GAME");
16
         System.out.println("GUESSING GAME");
17
-        
17
+
18
         Random rand = new Random();
18
         Random rand = new Random();
19
-        int answer = rand.nextInt(10) + 1;
20
-        int numGuesses = 1;
19
+        int answer = rand.nextInt(9) + 1;
20
+        int numGuesses = 0;
21
         int userGuess = 0;
21
         int userGuess = 0;
22
-        
23
-        // Print mystery number for check
24
-        // System.out.println(answer);
25
-        
22
+        int lastGuess = 0;
23
+
24
+        boolean[] guessArray = new boolean[10];
25
+
26
+        for (int i = 0; i < 10; i++){
27
+            guessArray[i] = false;
28
+            //System.out.println(guessArray[i]);
29
+        };
30
+
26
         while (userGuess != answer){
31
         while (userGuess != answer){
27
-            
28
             Scanner scan = new Scanner(System.in);
32
             Scanner scan = new Scanner(System.in);
29
-            System.out.println("Guess a number from 1 to 10:");
33
+            System.out.println("Guess a number from 0 to 9:");
30
             userGuess = scan.nextInt();
34
             userGuess = scan.nextInt();
31
-        
32
-            if (userGuess == answer) {
33
-                System.out.println("correct guess in " + numGuesses + " guesses!!");
34
-            } else {
35
-                if (userGuess < answer){
36
-                    System.out.println("too small");
37
-                } else {
38
-                    System.out.println("too large");
35
+
36
+            if (userGuess >= 0 && userGuess < 10){
37
+
38
+                for (int i = 0; i < 10; i++){
39
+                    if(guessArray[userGuess] == false){
40
+
41
+                        guessArray[userGuess] = true;
42
+                        numGuesses++;
43
+
44
+                    } 
45
+                    //Prints all boolean values of array
46
+                    //System.out.println(guessArray[i]);
39
                 };
47
                 };
40
-                numGuesses++;
48
+
49
+
50
+                if (userGuess == answer) {
51
+                    System.out.println("You guessed the mystery number " + answer + " correct ");
52
+                    System.out.println("in " + numGuesses + " guesses!!");
53
+                } else {
54
+                    if (userGuess < answer){
55
+                        System.out.println("too small");
56
+                    } else {
57
+                        System.out.println("too large");
58
+                    };
59
+                    
60
+                    //Testing
61
+                    //System.out.println("userGuess is: " + userGuess);
62
+                    //System.out.println("lastGuess is: " + lastGuess);
63
+                    //System.out.println("numGuesses is: " + numGuesses);
64
+
65
+                }
66
+
67
+                lastGuess = userGuess;
68
+            } else {
69
+                System.out.println("You guessed out of the range!  Guess again!!");
41
             }
70
             }
42
-    
43
         }
71
         }
44
     }
72
     }
45
 }
73
 }
74
+

+ 6
- 6
TooLargeTooSmall/package.bluej View File

1
 #BlueJ package file
1
 #BlueJ package file
2
-editor.fx.0.height=722
3
-editor.fx.0.width=800
4
-editor.fx.0.x=425
5
-editor.fx.0.y=23
2
+editor.fx.0.height=952
3
+editor.fx.0.width=891
4
+editor.fx.0.x=1791
5
+editor.fx.0.y=-186
6
 objectbench.height=101
6
 objectbench.height=101
7
 objectbench.width=776
7
 objectbench.width=776
8
 package.divider.horizontal=0.6
8
 package.divider.horizontal=0.6
9
 package.divider.vertical=0.8007380073800738
9
 package.divider.vertical=0.8007380073800738
10
 package.editor.height=427
10
 package.editor.height=427
11
 package.editor.width=674
11
 package.editor.width=674
12
-package.editor.x=23
13
-package.editor.y=136
12
+package.editor.x=1335
13
+package.editor.y=-91
14
 package.frame.height=600
14
 package.frame.height=600
15
 package.frame.width=800
15
 package.frame.width=800
16
 package.numDependencies=0
16
 package.numDependencies=0