Kaynağa Gözat

Working program of the guessing game

Trinh Tong 6 yıl önce
ebeveyn
işleme
24a461a88c

BIN
TooLargeTooSmall/TooLargeTooSmall.class Dosyayı Görüntüle


+ 7
- 0
TooLargeTooSmall/TooLargeTooSmall.ctxt Dosyayı Görüntüle

@@ -0,0 +1,7 @@
1
+#BlueJ class context
2
+comment0.target=TooLargeTooSmall
3
+comment1.params=
4
+comment1.target=int\ randomNumGenerator()
5
+comment2.params=
6
+comment2.target=void\ guessing()
7
+numComments=3

+ 93
- 0
TooLargeTooSmall/TooLargeTooSmall.java Dosyayı Görüntüle

@@ -0,0 +1,93 @@
1
+
2
+/**
3
+* Guessing game 
4
+* prompts user to guess a mystery number
5
+* after every guess, "Too large" "Too small" "Correct"
6
+* when terminating, display # of guesses
7
+* if the same number is used, it counts as 1 guess
8
+*/
9
+
10
+import java.util.*;
11
+
12
+public class TooLargeTooSmall {
13
+
14
+    public static int randomNumGenerator(){
15
+        // this section starts the game by letting the user select
16
+        // a game mode
17
+        Random num = new Random();
18
+        Scanner keyboard = new Scanner(System.in);
19
+        System.out.println("Welcome to the guessing game... \n"
20
+                            + "Select a mode \n" 
21
+                            + "1: Easy (Number between 1-10)\n"
22
+                            + "2: Normal (Number between 1-20)\n"
23
+                            + "3: Hard (Number between 1-50)\n"
24
+                            + "Select by inputing the number");
25
+                            
26
+        int gameMode = keyboard.nextInt();
27
+        
28
+        int min = 0;
29
+        int max = 0;
30
+    
31
+        switch (gameMode) {
32
+            
33
+            case 2: min = 1;
34
+                    max = 20;
35
+                    break;
36
+            case 3: min = 1;
37
+                    max = 50;
38
+                    break;
39
+            default: min = 1;
40
+                     max = 10;
41
+                     break;
42
+            
43
+        }
44
+        
45
+        int guessMe = num.nextInt((max - min) + 1) + min;
46
+        
47
+        return guessMe;
48
+    }
49
+
50
+    public void guessing() {
51
+        
52
+        int numberToGuess = randomNumGenerator();
53
+    
54
+        boolean guessFound = false;
55
+        
56
+        int guessCounter = 0,
57
+            previousGuess = 0;
58
+        
59
+        Scanner keyboard = new Scanner(System.in);
60
+        // While loop until guess is correct.
61
+        // interacts with other methods
62
+        // guess checker - will check guess against other guesses
63
+        // boolean, if currentGuess = previousGuess, repeatGuess = true/false
64
+        
65
+        while (guessFound == false) {
66
+            // guess found - returns win message and how many guesses
67
+            System.out.println("What's your guess?!");
68
+            int currentGuess = keyboard.nextInt();
69
+            
70
+            // if statement to check if guess is correct
71
+            
72
+            if (currentGuess == numberToGuess) {
73
+                System.out.println("You guessed the right number!" 
74
+                                    + "\n" 
75
+                                    + "It took you this many guesses: "
76
+                                    + guessCounter);
77
+                guessFound = true;
78
+
79
+            } else {
80
+                // check guess
81
+                if (currentGuess != previousGuess) {
82
+                    guessCounter++;
83
+                    previousGuess = currentGuess;
84
+                }
85
+                System.out.println("That wasn't the right answer. Guess again!");
86
+            }
87
+        }
88
+        
89
+        System.out.println("");
90
+        
91
+    }
92
+    
93
+}

+ 30
- 1
TooLargeTooSmall/package.bluej Dosyayı Görüntüle

@@ -1,3 +1,32 @@
1 1
 #BlueJ package file
2
-#Thu Oct 18 08:30:23 EDT 2018
2
+editor.fx.0.height=722
3
+editor.fx.0.width=800
4
+editor.fx.0.x=320
5
+editor.fx.0.y=75
6
+objectbench.height=101
7
+objectbench.width=776
8
+package.divider.horizontal=0.6
9
+package.divider.vertical=0.8007380073800738
10
+package.editor.height=427
11
+package.editor.width=674
12
+package.editor.x=82
13
+package.editor.y=293
14
+package.frame.height=600
15
+package.frame.width=800
16
+package.numDependencies=0
17
+package.numTargets=1
18
+package.showExtends=true
19
+package.showUses=true
3 20
 project.charset=UTF-8
21
+readme.height=58
22
+readme.name=@README
23
+readme.width=47
24
+readme.x=10
25
+readme.y=10
26
+target1.height=50
27
+target1.name=TooLargeTooSmall
28
+target1.showInterface=false
29
+target1.type=ClassTarget
30
+target1.width=140
31
+target1.x=70
32
+target1.y=10