|
@@ -1,9 +1,59 @@
|
|
1
|
+
|
1
|
2
|
/**
|
2
|
|
- * Created by iyasuwatts on 10/17/17.
|
|
3
|
+ * Write a description of class main here.
|
|
4
|
+ *
|
|
5
|
+ * @author (your name)
|
|
6
|
+ * @version (a version number or a date)
|
3
|
7
|
*/
|
4
|
|
-public class Main {
|
5
|
8
|
|
6
|
|
- public static void main(String[] args){
|
7
|
|
-
|
|
9
|
+import java.util.Scanner;
|
|
10
|
+import java.util.Random;
|
|
11
|
+import java.util.InputMismatchException;
|
|
12
|
+
|
|
13
|
+public class main
|
|
14
|
+{
|
|
15
|
+ static Scanner in = new Scanner(System.in);
|
|
16
|
+ public static void main(String[] args ){
|
|
17
|
+ Random rand = new Random();
|
|
18
|
+ int mysteryNumber = rand.nextInt(100);
|
|
19
|
+ int count=0;
|
|
20
|
+ int guess=-1;
|
|
21
|
+ int previousGuess=-1;
|
|
22
|
+ System.out.println("Let\'s guess a number between 0 and 99: ");
|
|
23
|
+ while (guess != mysteryNumber) {
|
|
24
|
+ guess = GetAnInteger();
|
|
25
|
+ if (guess < 0 || guess>99) {
|
|
26
|
+ System.out.println("Invalid input! Try again!");
|
|
27
|
+ } else if (guess > mysteryNumber) {
|
|
28
|
+ System.out.println("Too large!");
|
|
29
|
+ } else if (guess < mysteryNumber) {
|
|
30
|
+ System.out.println("Too small!");
|
|
31
|
+ } else if (guess == mysteryNumber) {
|
|
32
|
+ System.out.println("You got it!");
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ //don't count consecutive and invalid inputs
|
|
36
|
+ if (guess != previousGuess && guess >= 0 && guess<=99) {
|
|
37
|
+ count++;
|
|
38
|
+ }
|
|
39
|
+ previousGuess = guess;
|
|
40
|
+ }
|
|
41
|
+ System.out.println("You\'ve made " + count + " tries before successful guess");
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ public static int GetAnInteger()
|
|
45
|
+ {
|
|
46
|
+ while (true)
|
|
47
|
+ {
|
|
48
|
+ try
|
|
49
|
+ {
|
|
50
|
+ return in.nextInt();
|
|
51
|
+ }
|
|
52
|
+ catch (InputMismatchException e)
|
|
53
|
+ {
|
|
54
|
+ in.next();
|
|
55
|
+ System.out.println("Invalid input! Try again!");
|
|
56
|
+ }
|
|
57
|
+ }
|
8
|
58
|
}
|
9
|
59
|
}
|