Parcourir la source

Number guessing program (edited original Main.java file as well as created new BlueJ project - same thing)

Jennifer Chao il y a 6 ans
Parent
révision
3827fed48b

BIN
.DS_Store Voir le fichier


+ 54
- 2
Main.java Voir le fichier

@@ -1,9 +1,61 @@
1 1
 /**
2 2
  * Created by iyasuwatts on 10/17/17.
3 3
  */
4
+
5
+import java.util.Scanner;
6
+import java.util.Random;
7
+
4 8
 public class Main {
5 9
 
6
-    public static void main(String[] args){
7
-        
10
+    public static void main(String[] args) {
11
+
12
+        // Create random number from range of 0 to 100.
13
+        Random rand = new Random();
14
+        int mysteryNum = rand.nextInt(101);
15
+
16
+        // Log number of user inputs.
17
+        int userAttempts = 1;
18
+
19
+        int userGuess;
20
+        int prevGuess = 101;
21
+
22
+        // Continue looping infinitely until correct answer is passed. Count each attempt.
23
+        do {
24
+            for (; ; userAttempts++) {
25
+                Scanner input = new Scanner(System.in);
26
+
27
+                System.out.println("What positive integer between 0 and 100 am I thinking of?");
28
+                userGuess = input.nextInt();
29
+
30
+                System.out.println("You guessed: " + userGuess);
31
+
32
+                // Check what the mystery number is. (personal use for testing)
33
+                // System.out.println(mysteryNum);
34
+
35
+                // Empty variable for storing the previous guess to compare.
36
+
37
+
38
+                // Conditionals (correct, too high, too low, same guess, out of bounds)
39
+
40
+                if (mysteryNum == userGuess) {
41
+                    System.out.println("Correct! Number of attempts: " + userAttempts);
42
+                    break;
43
+                } else if (userGuess > 100 || userGuess < 0) {
44
+                    System.out.println("Is that number between 0 and 100?");
45
+                } else if (prevGuess == userGuess) {
46
+                    System.out.println("That's the same number!");
47
+                    userAttempts -= 1;
48
+                } else if (mysteryNum > userGuess) {
49
+                    System.out.println("Too low! Try again!");
50
+                } else if (mysteryNum < userGuess) {
51
+                    System.out.println("Too high! Try again!");
52
+                }
53
+
54
+                // Save previous guess for comparison.
55
+                prevGuess = userGuess;
56
+            }
57
+
58
+        } while (mysteryNum != userGuess);
59
+
8 60
     }
9 61
 }

BIN
TooLargeTooSmall/Main.class Voir le fichier


+ 5
- 0
TooLargeTooSmall/Main.ctxt Voir le fichier

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

+ 56
- 0
TooLargeTooSmall/Main.java Voir le fichier

@@ -0,0 +1,56 @@
1
+import java.util.Scanner;
2
+import java.util.Random;
3
+
4
+public class Main {
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
+        int userGuess;
16
+        int prevGuess = 101;
17
+
18
+        // Continue looping infinitely until correct answer is passed. Count each attempt.
19
+        do {
20
+            for (; ; userAttempts++) {
21
+                Scanner input = new Scanner(System.in);
22
+
23
+                System.out.println("What positive integer between 0 and 100 am I thinking of?");
24
+                userGuess = input.nextInt();
25
+
26
+                System.out.println("You guessed: " + userGuess);
27
+
28
+                // Check what the mystery number is. (personal use for testing)
29
+                // System.out.println(mysteryNum);
30
+
31
+                // Empty variable for storing the previous guess to compare.
32
+
33
+
34
+                // Conditionals (correct, too high, too low, same guess, out of bounds)
35
+
36
+                if (mysteryNum == userGuess) {
37
+                    System.out.println("Correct! Number of attempts: " + userAttempts);
38
+                    break;
39
+                } else if (userGuess > 100 || userGuess < 0) {
40
+                    System.out.println("Is that number between 0 and 100?");
41
+                } else if (prevGuess == userGuess) {
42
+                    System.out.println("That's the same number!");
43
+                    userAttempts -= 1;
44
+                } else if (mysteryNum > userGuess) {
45
+                    System.out.println("Too low! Try again!");
46
+                } else if (mysteryNum < userGuess) {
47
+                    System.out.println("Too high! Try again!");
48
+                }
49
+
50
+                // Save previous guess for comparison.
51
+                prevGuess = userGuess;
52
+            }
53
+
54
+        } while (mysteryNum != userGuess);
55
+    }
56
+}

+ 0
- 0
TooLargeTooSmall/README.TXT Voir le fichier


+ 37
- 0
TooLargeTooSmall/README.md Voir le fichier

@@ -0,0 +1,37 @@
1
+# ZCW-MicroLabs-JavaFundamentals-AliceAndBob
2
+
3
+## `Fork` this Repository
4
+* You should work on this project in your own repository.
5
+* Click the `fork` button in the top right corner to create a copy of this repository on your github account.
6
+* You can go through the [GitHub forking tutorial](https://help.github.com/articles/fork-a-repo/) if you need additional practice with this.
7
+
8
+
9
+## Alice and Bob Greeting
10
+
11
+### **Objective:**
12
+* Write a program which prompts the user to input his/her name.
13
+* The program should greet users whose names are 'Alice' and 'Bob'.
14
+
15
+### **Purpose:**
16
+* To establish familiarity with
17
+    * Control Flow
18
+    * Conditionals
19
+    * User input
20
+    * Object instantation/declaration
21
+    * Method invokation
22
+
23
+### **Resources:**
24
+* [Procedural Programming](https://zipcoder.github.io/reveal-slides-light/procedural-programming.html#/)
25
+
26
+### Unit Test
27
+* No Unit Test
28
+
29
+
30
+### Instructions
31
+1. Understand how to get input from user
32
+2. Create conditional to check against Alice and Bob
33
+3. Print greeting to screen if Alice or Bob are true
34
+
35
+
36
+## What's next?
37
+* The next lab can be founds [here](https://github.com/Zipcoder/ZCW-MicroLabs-JavaFundamentals-SumOfInput).

+ 32
- 0
TooLargeTooSmall/package.bluej Voir le fichier

@@ -0,0 +1,32 @@
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
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=426
13
+package.editor.y=53
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
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=Main
28
+target1.showInterface=false
29
+target1.type=ClassTarget
30
+target1.width=80
31
+target1.x=150
32
+target1.y=60