-
Notifications
You must be signed in to change notification settings - Fork 13
/
SameTwoDiceAppSimple.java
43 lines (31 loc) · 986 Bytes
/
SameTwoDiceAppSimple.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
package lessons.oop.basics.randomclass;
public class SameTwoDiceAppSimple {
public static void main(String[] args)
{
Test.run();
}
}
class Test {
public static void run()
{
for (;;) {
System.out.print("How many time you want to dice:");
int diceCount = new java.util.Scanner(System.in).nextInt();
if (diceCount == 0)
return;
System.out.printf("%d per times rolling dice percent: %%%.3f%n", diceCount,
SameTwoDiceCalculation.sameDiceChangeRatio(diceCount, new java.util.Random()) * 100);
}
}
}
class SameTwoDiceCalculation {
public static double sameDiceChangeRatio(int loopTimes, java.util.Random r)
{
int diceOne = r.nextInt(6) + 1;
int count = 0;
for (int i = 3; i <= loopTimes + 3; ++i)
if (r.nextInt(6) + 1 == diceOne)
++count;
return (double)count / loopTimes;
}
}