-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh3Ex2.java
More file actions
49 lines (47 loc) · 1.16 KB
/
Ch3Ex2.java
File metadata and controls
49 lines (47 loc) · 1.16 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
//Simulates 10,000 games of craps and gives probablity
import java.util.Scanner;
import java.util.Random;
public class Ch3Ex2
{
public static void main(String[] args)
{
Random rand = new Random();
int winCount = 0;
int lossCount = 0;
for (int i = 0;i<10000;i++)
{
int rollDice = ((rand.nextInt(6)+ 1) + (rand.nextInt(6)+ 1));
if (rollDice == 7 || rollDice == 11)
{
winCount += 1;
}
else if (rollDice == 2 || rollDice == 3 || rollDice == 12)
{
lossCount += 1;
}
else
{
int point = rollDice;
while (true)
{
rollDice = ((rand.nextInt(6)+ 1) + (rand.nextInt(6)+ 1));
if (rollDice == point)
{
winCount += 1;
break;
}
if (rollDice == 7)
{
lossCount += 1;
break;
}
}
}
}
double prob = (winCount*1.0/(winCount + lossCount)*100.0);
System.out.println("Number of wins :"+winCount);
System.out.println("Number of losses :"+lossCount);
System.out.print("Game Winning Probablility Percent: ");
System.out.printf("%.2f",prob);
}
}