Ver código fonte

Updated to satisfy consecutive number condition

Kris Blassingame 6 anos atrás
pai
commit
a724cb9803
3 arquivos alterados com 9 adições e 12 exclusões
  1. BIN
      Main.class
  2. 8
    11
      Main.java
  3. 1
    1
      package.bluej

BIN
Main.class Ver arquivo


+ 8
- 11
Main.java Ver arquivo

8
         Scanner input = new Scanner(System.in);
8
         Scanner input = new Scanner(System.in);
9
 
9
 
10
         int mysteryNum = (int) Math.floor(Math.random() * 100 + 1);
10
         int mysteryNum = (int) Math.floor(Math.random() * 100 + 1);
11
-        int numOfGuesses =+ 1;
11
+        int numOfGuesses = 0;
12
+        int prevGuess = 0;
13
+        int userNum = 0;
12
         System.out.println("Hey, you! Pick a number between 1 and 100: ");
14
         System.out.println("Hey, you! Pick a number between 1 and 100: ");
13
-        int userNum = input.nextInt();
14
-        
15
+
15
         while (userNum != mysteryNum) {
16
         while (userNum != mysteryNum) {
17
+            userNum = input.nextInt();
16
             if (userNum < mysteryNum) {
18
             if (userNum < mysteryNum) {
17
                 System.out.println("Too small! Guess again!");
19
                 System.out.println("Too small! Guess again!");
18
-                numOfGuesses++;
19
-                userNum = input.nextInt();
20
             }
20
             }
21
-
22
             else if (userNum > mysteryNum) {
21
             else if (userNum > mysteryNum) {
23
                 System.out.println("Too large! Guess again!");
22
                 System.out.println("Too large! Guess again!");
24
-                userNum = input.nextInt();
25
-                numOfGuesses++;
26
             }
23
             }
27
-            else{
28
-
24
+            if (prevGuess != userNum) {
25
+                numOfGuesses++;
29
             }
26
             }
30
-
27
+            prevGuess = userNum;
31
         }
28
         }
32
         System.out.println("Correct guess!");
29
         System.out.println("Correct guess!");
33
         System.out.println("It took you " + numOfGuesses + " guess(es) to figure it out!");
30
         System.out.println("It took you " + numOfGuesses + " guess(es) to figure it out!");

+ 1
- 1
package.bluej Ver arquivo

2
 editor.fx.0.height=722
2
 editor.fx.0.height=722
3
 editor.fx.0.width=945
3
 editor.fx.0.width=945
4
 editor.fx.0.x=229
4
 editor.fx.0.x=229
5
-editor.fx.0.y=114
5
+editor.fx.0.y=115
6
 objectbench.height=297
6
 objectbench.height=297
7
 objectbench.width=504
7
 objectbench.width=504
8
 package.divider.horizontal=0.5997652582159625
8
 package.divider.horizontal=0.5997652582159625