import cse1030.Die; import java.util.Collections; import java.util.List; import java.util.ArrayList; public class Lab01B { public static void main(String[] args) { final int N = 5; // Create a list of N dice to roll List dice = new ArrayList(); for (int i = 0; i < N; i++) { dice.add(new Die()); } final int TRIALS = 1000000; int numberStraights = 0; for (int trial = 0; trial < TRIALS; trial++) { for (Die d : dice) { d.roll(); } // Sort the dice; this makes checking for the straight much easier! Collections.sort(dice); // For a large straight, we loop through the dice starting at the // die in the second position in the list and finishing at the die // in the last position of the list. // Each time through the loop we compare the die in the current // position to the die in the position before it; if the value // of the die in the current position is not one larger than the // die in the position before it there cannot be a straight. boolean isStraight = true; for (int i = 1; i < N; i++) { if (dice.get(i).getValue() != dice.get(i - 1).getValue() + 1) { isStraight = false; } } if (isStraight) { numberStraights++; } } System.out.printf("%.6f%n", (double) numberStraights / TRIALS); } }