Answer to Question #17752 in Java | JSP | JSF for Sayed Abdullah Almousawi

Question #17752
Probability: Rolling 6 sided dice can teach us about probability.
Allow the user to choose a number of rolls for 2 six sided dice. Use random number generator to produce the resulting rolls.

Add the dice together and keep track of how many of each of the sums are rolled. Print out a frequency distribution table and then a frequency histogram
1
Expert's answer
2012-11-02T10:35:54-0400
import java.util.Arrays;
import java.util.Scanner;

public class Dice {

public static void main(String[] args) {
& @SuppressWarnings("resource")
& Scanner input= new Scanner(System.in);
& System.out.print("Choose a number of rolls for 2 six sided dice: ");
& int in=input.nextInt();
& int[] summarr = new int [12];
& Arrays.fill(summarr,0);
& for (int i = 0; i<in; i++) {
& summarr[throwDice() + throwDice() - 1]++;
& }
& for (int i = 2; i<13; i++) {
& System.out.printf("%2d& %d",i,summarr[i-1]);
& System.out.println();
&
& }
}
public static int throwDice () {
& return ((int) (Math.random() *6) + 1) ;
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
14.03.14, 14:50

Dear sarah You're welcome. We are glad to be helpful. If you really liked our service please press like-button beside answer field. Thank you!

sarah
12.03.14, 20:03

thanks

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS