|
@@ -1,3 +1,7 @@
|
|
1
|
+import java.io.File;
|
|
2
|
+import java.io.FileNotFoundException;
|
|
3
|
+import java.io.PrintStream;
|
|
4
|
+
|
1
|
5
|
public class Simulation {
|
2
|
6
|
|
3
|
7
|
int dice = 0;
|
|
@@ -20,4 +24,37 @@ public class Simulation {
|
20
|
24
|
}
|
21
|
25
|
}
|
22
|
26
|
}
|
|
27
|
+
|
|
28
|
+ public void printToFile() throws FileNotFoundException {
|
|
29
|
+ // Output to file
|
|
30
|
+
|
|
31
|
+ PrintStream file = new PrintStream(new File("trinhResults.md"));
|
|
32
|
+ PrintStream console = System.out;
|
|
33
|
+
|
|
34
|
+ System.setOut(file);
|
|
35
|
+
|
|
36
|
+ StringBuilder resultsOutput = new StringBuilder();
|
|
37
|
+
|
|
38
|
+ resultsOutput.append("***\nSimulation of " + this.dice + " dice tossed for " + this.tosses + ".\n***\n\n");
|
|
39
|
+
|
|
40
|
+ for (int key: simulationBins.allKeys) {
|
|
41
|
+ int value = simulationBins.results.get(key);
|
|
42
|
+ resultsOutput.append(String.format("%3d : %8d: %4.2f ", key, value, ((double) value / (double) this.tosses)));
|
|
43
|
+ int starCounter = (int) Math.floor((double) ( value / ( this.tosses / 100)));
|
|
44
|
+ resultsOutput.append(repeatString("*", starCounter) + "\n");
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ System.out.println(resultsOutput.toString());
|
|
48
|
+
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ public String repeatString(String stringToRepeat, int numberOfTimes){
|
|
52
|
+ String returnString = "";
|
|
53
|
+
|
|
54
|
+ for (int i = 0; i < numberOfTimes; i++) {
|
|
55
|
+ returnString += stringToRepeat;
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ return returnString;
|
|
59
|
+ }
|
23
|
60
|
}
|