1 |
/** DiceUntil9 - keeps throwing dice until 9 is rolled. |
1 |
2 |
**/ |
2 |
3 |
public class DiceUntil9 |
3 |
4 |
{ |
4 |
5 |
public static void main
(String [] args) |
5 |
6 |
{ |
6 |
7 |
double x; // holds the random number we will generate |
7 |
8 |
int diceValue; // holds
the value of the dice that we calculate from the random number |
8 |
9 |
int diceCount = 0; |
9 |
10 |
int diceTotal = 0; |
10 |
11 |
int win = 0; |
11 |
12 |
int loss = 0; |
12 |
13 |
int throwCount = 0; |
13 |
14 |
while (diceTotal !=
9) // keep rolling until 9 |
14 |
15 |
{ |
15 |
16 |
throwCount++; |
16 |
17 |
diceTotal = 0; |
17 |
18 |
|
18 |
19 |
// roll one dice
2x |
19 |
20 |
for(diceCount =
0; diceCount < 2; diceCount++) |
20 |
21 |
{ |
21 |
22 |
// roll one
die |
22 |
23 |
x =
Math.random(); // generates a number between 0 and 1 |
23 |
24 |
x = x *6;
// multiplies that number times
6 so the number will be from 0 to 5 |
24 |
25 |
diceValue =
(int) x; // convert it to integer to chop off the decimal portion |
25 |
26 |
diceValue =
diceValue +1; // with dice, we want 1-6, not 0-5, so we add 1 |
26 |
27 |
System.out.println ("The value of this die is " +
diceValue); |
27 |
28 |
diceTotal =
diceValue + diceTotal; // sum the
dice |
28 |
29 |
} |
29 |
30 |
System.out.println("The two dice together equal " +
diceTotal); |
30 |
31 |
} |
31 |
32 |
//print the number of
throws it took to win |
32 |
33 |
System.out.println("IT took you
" + throwCount + "rolls to win."); |
33 |
34 |
} |
34 |
35 |
} |
35 |
|
|
|