-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiceSimWhile.java
80 lines (67 loc) · 2.26 KB
/
DiceSimWhile.java
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
//Captain-Price-TF-141
import java.util.Random; // Needed for the Random class
/**
This class simulates rolling a pair of dice 10,000 times
and counts the number of times doubles of are rolled for
each different pair of doubles.
*/
public class DiceSimWhile
{
public static void main(String[] args)
{
final int NUMBER = 10000; // Number of dice rolls
// A random number generator used in
// simulating the rolling of dice
Random generator = new Random();
int die1Value; // Value of the first die
int die2Value; // Value of the second die
int count = 0; // Total number of dice rolls
int snakeEyes = 0; // Number of snake eyes rolls
int twos = 0; // Number of double two rolls
int threes = 0; // Number of double three rolls
int fours = 0; // Number of double four rolls
int fives = 0; // Number of double five rolls
int sixes = 0; // Number of double six rolls
// TASK #1 Enter your code for the algorithm here
while(count < NUMBER)
{
die1Value = generator.nextInt(7);
die2Value = generator.nextInt(7);
if(die1Value == die2Value)
{
if(die1Value == 1)
{
snakeEyes++;
}
else if(die1Value == 2)
{
twos++;
}
else if(die1Value == 3)
{
threes++;
}
else if(die1Value == 4)
{
fours++;
}
else if(die1Value == 5)
{
fives++;
}
else if(die1Value == 6)
{
sixes++;
}
}
count++;
}
// Display the results
System.out.println ("You rolled snake eyes " + snakeEyes + " out of " + count + " rolls.");
System.out.println ("You rolled double twos " + twos + " out of " + count + " rolls.");
System.out.println ("You rolled double threes " + threes + " out of " + count + " rolls.");
System.out.println ("You rolled double fours " + fours + " out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives + " out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes + " out of " + count + " rolls.");
}
}