repl.it
@sthomason1/

Dice rolling challenge

Java

No description

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int[] tally = new int[13]; //tally[0] = total rolls
    System.out.println("Number of trials?");
    int trials = scn.nextInt();
    for (int i = 0; i < trials; i++) {
      while (tally[7] == i) {
        int roll1 = (int) Math.floor(1+6*Math.random());
        int roll2 = (int) Math.floor(1+6*Math.random());
        tally[0]++;
        tally[roll1 + roll2]++;
      }
    }
    double avg = ((double) tally[0]) / trials;
    System.out.printf("It took an average of %.3f trials\n", avg);
    for (int i = 2; i < 13; i++) {
      System.out.println("Frequency of " + i + "s: " + tally[i] + " / " + tally[0]);
    }
  }
}
Fetching token
?