|
@@ -0,0 +1,61 @@
|
|
1
|
+
|
|
2
|
+/**
|
|
3
|
+ * Write a description of class GuessGame here.
|
|
4
|
+ *
|
|
5
|
+ * @author (your name)
|
|
6
|
+ * @version (a version number or a date)
|
|
7
|
+ */
|
|
8
|
+import java.util.Random;
|
|
9
|
+import java.util.Scanner;
|
|
10
|
+public class GuessGame
|
|
11
|
+{
|
|
12
|
+ // instance variables - replace the example below with your own
|
|
13
|
+ private int x;
|
|
14
|
+
|
|
15
|
+ /**
|
|
16
|
+ * Constructor for objects of class GuessGame
|
|
17
|
+ */
|
|
18
|
+ public static void main(String[] args){
|
|
19
|
+ System.out.println("Hello and welcome to my number guessing game.");
|
|
20
|
+ System.out.println("Pick a number 1 through 10: ");
|
|
21
|
+
|
|
22
|
+ Scanner input = new Scanner(System.in);
|
|
23
|
+ int guess;
|
|
24
|
+ int counter = 0;
|
|
25
|
+ int prevGuess = -1;
|
|
26
|
+
|
|
27
|
+ int rand = (int) (Math.random()*10)+1;
|
|
28
|
+ boolean notCorrect = true;
|
|
29
|
+ while(notCorrect){
|
|
30
|
+ guess = input.nextInt();
|
|
31
|
+
|
|
32
|
+ if(guess != prevGuess){
|
|
33
|
+ counter++;
|
|
34
|
+ }
|
|
35
|
+ prevGuess = guess;
|
|
36
|
+ if(guess > rand){
|
|
37
|
+ System.out.println("Your guess is too high");
|
|
38
|
+ } else if(guess < rand){
|
|
39
|
+ System.out.println("Your guess is too low");
|
|
40
|
+ }else {
|
|
41
|
+ notCorrect = false;
|
|
42
|
+ System.out.println("You got it! " + counter + " guesses");
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ }
|
|
46
|
+ /*if (guess == number){
|
|
47
|
+ System.out.println("You got it!");
|
|
48
|
+ }
|
|
49
|
+ else if (guess < number){
|
|
50
|
+ System.out.println("Your guess is too low");
|
|
51
|
+
|
|
52
|
+ }
|
|
53
|
+ else if (guess > number){
|
|
54
|
+ System.out.println("Your guess is too high");
|
|
55
|
+
|
|
56
|
+ }*/
|
|
57
|
+
|
|
58
|
+ }
|
|
59
|
+}
|
|
60
|
+
|
|
61
|
+
|