Просмотр исходного кода

FINISHED TooLargeTooSmall ADDED FUNCTIONALITY

Nathan Hall 6 лет назад
Родитель
Сommit
7fd07b9ce3
3 измененных файлов: 56 добавлений и 27 удалений
  1. Двоичные данные
      TooLargeTooSmall/GuessingGame.class
  2. 50
    21
      TooLargeTooSmall/GuessingGame.java
  3. 6
    6
      TooLargeTooSmall/package.bluej

Двоичные данные
TooLargeTooSmall/GuessingGame.class Просмотреть файл


+ 50
- 21
TooLargeTooSmall/GuessingGame.java Просмотреть файл

@@ -10,36 +10,65 @@ import java.util.Scanner;
10 10
 import java.util.Random;
11 11
 
12 12
 public class GuessingGame{
13
-    
13
+
14 14
     public static void main(String[] args) {
15
-    
15
+
16 16
         System.out.println("GUESSING GAME");
17
-        
17
+
18 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 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 31
         while (userGuess != answer){
27
-            
28 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 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 Просмотреть файл

@@ -1,16 +1,16 @@
1 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 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=23
13
-package.editor.y=136
12
+package.editor.x=1335
13
+package.editor.y=-91
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0