Design a Java program which can show following results How m
Design a Java program which can show following results:
How many times should the dice be rolled? 5
Die : 3
Die : 3
Die : 2
Die : 3
Die : 2
Occurrence counts for each face of the single die were:
Face 1: 0
Face 2: 2
Face 3: 3
Face 4: 0
Face 5: 0
Face 6: 0
Solution
DiceTest.java
import java.util.Random;
import java.util.Scanner;
public class DiceTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print(\"How many times should the dice be rolled? \");
int n = scan.nextInt();
int count[] = {0,0,0,0,0,0};
Random r = new Random();
for(int i=0; i<n;i++){
int die = r.nextInt(6)+1;
System.out.println(\"Die : \"+die);
count[die-1]++;
}
System.out.println(\"Occurrence counts for each face of the single die were: \");
for(int i=0; i<count.length; i++){
System.out.println(\"Face \"+(i+1)+\": \"+count[i]);
}
}
}
Output:
How many times should the dice be rolled? 10
Die : 1
Die : 6
Die : 3
Die : 5
Die : 1
Die : 1
Die : 5
Die : 5
Die : 5
Die : 5
Occurrence counts for each face of the single die were:
Face 1: 3
Face 2: 0
Face 3: 1
Face 4: 0
Face 5: 5
Face 6: 1

