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
|
// File :flow-switch/craps/CrapsDialog.java
// Purpose: A simple Craps program to show use of switch and loops.
// Author : Fred Swartz, April 2007, Placed in the public domain.
import javax.swing.*;
public class CrapsDialog {
public static void main(String[] args) {
//... Variables to hold the results of rolling the dice.
int die1;
int die2;
int total;
//... Counters for total number of wins and loses.
int won = 0;
int lost = 0;
//... Keep track of the user response.
int response;
JOptionPane.showMessageDialog(null, "Are you ready to start?");
do {
//... Roll the dice.
die1 = (int) (Math.random() * 6) + 1;
die2 = (int) (Math.random() * 6) + 1;
total = die1 + die2;
JOptionPane.showMessageDialog(null, "You rolled a " + die1 +
" and a " + die2 +
" for a total of " + total);
//... Decide what the result is: win, lose, play for point.
switch (total) {
case 7:
case 11:
//... WIN
won++;
JOptionPane.showMessageDialog(null, "Congratulations, you win!");
break;
case 2:
case 3:
case 12:
//... LOSE
lost++;
JOptionPane.showMessageDialog(null, "Sorry, you lose.");
break;
default:
//... PLAY FOR POINT
int point = total;
JOptionPane.showMessageDialog(null, "You must now roll for your point: " + point);
do {
//... Roll the dice.
die1 = (int) (Math.random() * 6) + 1;
die2 = (int) (Math.random() * 6) + 1;
total = die1 + die2;
String comment = "";
if (total == point) {
won++;
comment = "You win.";
} else if (total == 7) {
lost++;
comment = "You lose";
} else {
comment = "You need to roll again to try to make your point.";
}
JOptionPane.showMessageDialog(null, "Your point is " + point +
"\nYou rolled a " + die1 + " and a " + die2 +
" for a total of " + total + "\n" + comment);
} while (total != point && total != 7);
}
response = JOptionPane.showConfirmDialog(null, "Wins = " + won + ", lost = " + lost + " Play again?");
} while (response == JOptionPane.YES_OPTION);
}
}
|