-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
97 lines (67 loc) · 2.6 KB
/
GuessingGame.java
File metadata and controls
97 lines (67 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.*;
public class GuessingGame {
public static final int RANGE = 10;
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
char play = 'y';
int num_games = 0;
int total_guesses = 0;
int best = 0;
introduction();
while (play == 'y') {
int guesses = game(console);
total_guesses += guesses;
num_games++;
if (num_games == 1) {
best = guesses;
}
if (guesses < best) {
best = guesses;
}
System.out.print("Do you want to play again? ");
String ans = console.next();
play = ans.toLowerCase().charAt(0);
System.out.println();
}
stats(num_games, total_guesses, ((double)total_guesses/num_games), best);
}
public static void introduction() {
System.out.printf("This program allows you to play a guessing game.\n" +
"I will think of a number between 1 and %d\n" +
"and will allow you to guess until you get it.\n" +
"For each guess, I will tell you whether the right\n" +
"answer is higher or lower than your guess.\n", RANGE);
}
public static int game(Scanner console) {
Random rand = new Random();
int num = rand.nextInt(RANGE);
System.out.println(num);
System.out.printf("I'm thinking of a number between 1 and %d...\n", RANGE);
System.out.print("Your guess? ");
int guess = console.nextInt();
int count = 1;
while (guess != num) {
if (guess > num) {
System.out.println("It's lower");
} else {
System.out.println("It's higher");
}
count++;
System.out.print("Your guess? ");
guess = console.nextInt();
}
if (count == 1) {
System.out.println("You got it right on the first guess!!");
} else {
System.out.printf("You got it right in %d guesses\n", count);
}
return count;
}
public static void stats(int num_games, int total_guesses, double average, int best) {
System.out.println("Game statistics:");
System.out.println("\t total games = " + num_games);
System.out.println("\t total guesses = " + total_guesses);
System.out.println("\t guesses/game = " + average);
System.out.println("\t best game = " + best);
}
}