|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
|
2
|
+/**
|
|
|
3
|
+ * Guessing game: TooLargeTooSmall.
|
|
|
4
|
+ *
|
|
|
5
|
+ * @author (Chistian)
|
|
|
6
|
+ * @version (version1 10-19-18)
|
|
|
7
|
+ */
|
|
|
8
|
+import java.util.*;
|
|
|
9
|
+import java.util.Scanner;
|
|
|
10
|
+public class Main {
|
|
|
11
|
+
|
|
|
12
|
+ public static void main(String[] args){
|
|
|
13
|
+ Random rand = new Random();
|
|
|
14
|
+ int n = rand.nextInt(10) + 1;
|
|
|
15
|
+ Scanner in = new Scanner(System.in);
|
|
|
16
|
+ System.out.println("Please guess a number between 1 and 10");
|
|
|
17
|
+ int guess = in.nextInt();
|
|
|
18
|
+ int lastGuess = 0;
|
|
|
19
|
+ int numberOfGuesses=1;
|
|
|
20
|
+ while (n != guess){
|
|
|
21
|
+ if (n < guess) {
|
|
|
22
|
+ System.out.println("too large, guess another number");
|
|
|
23
|
+ if (guess != lastGuess){
|
|
|
24
|
+ lastGuess = guess;
|
|
|
25
|
+ numberOfGuesses++;}
|
|
|
26
|
+ }
|
|
|
27
|
+ else if(n > guess) {
|
|
|
28
|
+ System.out.println("too small, guess another number");
|
|
|
29
|
+ if (guess != lastGuess){
|
|
|
30
|
+ lastGuess = guess;
|
|
|
31
|
+ numberOfGuesses++;}
|
|
|
32
|
+
|
|
|
33
|
+ }
|
|
|
34
|
+ guess = in.nextInt();
|
|
|
35
|
+ }
|
|
|
36
|
+ System.out.println("correct! you took " +String.format("%d",numberOfGuesses)+ " guesses to get it right");
|
|
|
37
|
+ }
|
|
|
38
|
+}
|