Procházet zdrojové kódy

Dicey Lab FINISHED

Nathan Hall před 6 roky
rodič
revize
1981d2d7cb
4 změnil soubory, kde provedl 86 přidání a 1 odebrání
  1. 22
    1
      pom.xml
  2. 9
    0
      src/main/java/Bins.java
  3. 18
    0
      src/main/java/Dice.java
  4. 37
    0
      src/main/java/Simulation.java

+ 22
- 1
pom.xml Zobrazit soubor

@@ -8,5 +8,26 @@
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <build>
12
+        <plugins>
13
+            <plugin>
14
+                <groupId>org.apache.maven.plugins</groupId>
15
+                <artifactId>maven-compiler-plugin</artifactId>
16
+                <configuration>
17
+                    <source>1.8</source>
18
+                    <target>1.8</target>
19
+                </configuration>
20
+            </plugin>
21
+        </plugins>
22
+    </build>
23
+    <dependencies>
24
+        <dependency>
25
+            <groupId>junit</groupId>
26
+            <artifactId>junit</artifactId>
27
+            <version>4.12</version>
28
+            <scope>test</scope>
29
+        </dependency>
30
+    </dependencies>
31
+
32
+</project>
11 33
 
12
-</project>

+ 9
- 0
src/main/java/Bins.java Zobrazit soubor

@@ -1,4 +1,13 @@
1
+import java.util.*;
1 2
 
2 3
 public class Bins {
3 4
 
5
+    int[] bin = new int[13];
6
+
7
+    public int get(int i) {
8
+
9
+        return 0;
10
+    }
11
+
4 12
 }
13
+

+ 18
- 0
src/main/java/Dice.java Zobrazit soubor

@@ -1,4 +1,22 @@
1
+import java.util.*;
2
+
1 3
 public class Dice {
4
+    public int numDice = 2;
5
+    int sum = 0;
6
+
7
+
8
+    public Integer rollDice(int numDice, int sum) {
9
+        for (int i = 0; i < numDice; i++) {
10
+            int currentRoll = (int) ((Math.random() * 6) + 1);
11
+            sum += currentRoll;
12
+//        System.out.println("Roll is: " + currentRoll);
13
+//        System.out.println("Roll is: " + currentRoll2);
14
+        }
15
+        return sum;
16
+    }
2 17
 
3 18
 
4 19
 }
20
+
21
+
22
+

+ 37
- 0
src/main/java/Simulation.java Zobrazit soubor

@@ -1,5 +1,42 @@
1
+import java.util.Scanner;
2
+
1 3
 public class Simulation {
4
+    private static int numDice = 2;
5
+    private static int numberOfTosses = 1000000;
6
+    private static double percentage;
7
+    private static double stars;
8
+
9
+
10
+    public static void main(String[] args) {
11
+        Dice dice = new Dice();
12
+        Bins numberOfBin = new Bins();
13
+        int sum = 0;
14
+
15
+        for (int i = 0; i < numberOfTosses; i++) {
16
+            sum = dice.rollDice(numDice, 0);
17
+            ++numberOfBin.bin[sum];
18
+        }
19
+
20
+
21
+        //Print bins
22
+        for (int i = 2; i < 13; i++) {
23
+            //Percentage of rolls
24
+            percentage = ((numberOfBin.bin[i]) / numberOfTosses);
25
+            stars = (((double) numberOfBin.bin[i]) / numberOfTosses) * 100;
26
+
27
+//            System.out.println(numberOfBin.bin[i]);
28
+//            System.out.println("Bin " + i + ":\t" + numberOfBin.bin[i] + ":\t" + percentage);
29
+//            System.out.println("Bin " + i + ":\t" + numberOfBin.bin[i] + ":\t" + percentage);
30
+            System.out.printf("%2d :\t%6d:\t%.2f ", i, numberOfBin.bin[i], (double) numberOfBin.bin[i] / numberOfTosses);
31
+//            System.out.println((((double) numberOfBin.bin[i]) / numberOfTosses)*100);
32
+//            System.out.println((int)stars);
33
+            for (int j = 0; j < stars; j++) {
34
+                System.out.print("*");
35
+            }
36
+            System.out.println(" \n");
2 37
 
38
+        }
39
+    }
3 40
 
4 41
 
5 42
 }