|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| package num; |
|
22 |
| |
|
23 |
| import java.util.*; |
|
24 |
| |
|
25 |
| public class NumberGuessBean { |
|
26 |
| |
|
27 |
| int answer; |
|
28 |
| boolean success; |
|
29 |
| String hint; |
|
30 |
| int numGuesses; |
|
31 |
| |
|
32 |
0
| public NumberGuessBean() {
|
|
33 |
0
| reset();
|
|
34 |
| } |
|
35 |
| |
|
36 |
0
| public void setGuess(String guess) {
|
|
37 |
0
| numGuesses++;
|
|
38 |
| |
|
39 |
0
| int g;
|
|
40 |
0
| try {
|
|
41 |
0
| g = Integer.parseInt(guess);
|
|
42 |
| } |
|
43 |
| catch (NumberFormatException e) { |
|
44 |
0
| g = -1;
|
|
45 |
| } |
|
46 |
| |
|
47 |
0
| if (g == answer) {
|
|
48 |
0
| success = true;
|
|
49 |
| } |
|
50 |
0
| else if (g == -1) {
|
|
51 |
0
| hint = "a number next time";
|
|
52 |
| } |
|
53 |
0
| else if (g < answer) {
|
|
54 |
0
| hint = "higher";
|
|
55 |
| } |
|
56 |
0
| else if (g > answer) {
|
|
57 |
0
| hint = "lower";
|
|
58 |
| } |
|
59 |
| } |
|
60 |
| |
|
61 |
0
| public boolean getSuccess() {
|
|
62 |
0
| return success;
|
|
63 |
| } |
|
64 |
| |
|
65 |
0
| public String getHint() {
|
|
66 |
0
| return "" + hint;
|
|
67 |
| } |
|
68 |
| |
|
69 |
0
| public int getNumGuesses() {
|
|
70 |
0
| return numGuesses;
|
|
71 |
| } |
|
72 |
| |
|
73 |
0
| public void reset() {
|
|
74 |
0
| answer = Math.abs(new Random().nextInt() % 100) + 1;
|
|
75 |
0
| success = false;
|
|
76 |
0
| numGuesses = 0;
|
|
77 |
| } |
|
78 |
| } |