-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathQuestion.java
More file actions
73 lines (61 loc) · 1.56 KB
/
Question.java
File metadata and controls
73 lines (61 loc) · 1.56 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
package chapter12;
//An improved version of the "decision maker"
//program from chapter 9. This version uses an
//enum, rather than interface variables to
//represent the answers
import java.util.Random;
enum Answers {
NO, YES, MAYBE, LATER, SOON, NEVER
}
public class Question {
Random rand = new Random();
Answers ask() {
int prob = (int) (100 * rand.nextDouble());
if (prob < 15) {
return Answers.MAYBE;
}
if (prob < 30) {
return Answers.NO;
}
if (prob < 60) {
return Answers.YES;
}
if (prob < 75) {
return Answers.LATER;
}
if (prob < 98) {
return Answers.SOON;
} else return Answers.NEVER;
}
}
class AskMe {
static void answer(Answers result) {
switch (result) {
case NO:
System.out.println("No");
break;
case YES:
System.out.println("Yes");
break;
case MAYBE:
System.out.println("Maybe");
break;
case LATER:
System.out.println("Later");
break;
case SOON:
System.out.println("Soon");
break;
case NEVER:
System.out.println("Never");
break;
}
}
public static void main(String[] args) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}