Hi I wrote code for Java program to simulate the rolling of 2 dice. The application should roll the dice 36,000 times. In a tabular format it should show the frequency of rolling 2,3,4,5,6,7,8,9,10,11,12 and also the percentage. It compiles alright but I am not getting the output. Can someone help me find out why I am not getting the output. Thank you for your help. Here is the code:

``````// Program simulates rolling two six-sided dice 36,000 times.
import java.util.Random;
public class Roll36
{
// simulate rolling of dice 36000 times
public void rollDice()
{
Random randomNumbers = new Random();
int face1; // number on first die
int face2; // number on second die
int totals[] = new int[ 13 ]; // frequencies of the sums
// initialize totals to zero
for ( int index = 0; index < totals.length; index++ )
totals[ index ] = 0;
// roll the dice
for ( int roll = 1; roll <= 36000; roll++ ) {
face1 = 1 + randomNumbers.nextInt( 6 );
face2 = 1 + randomNumbers.nextInt( 6 );
totals[ face1 + face2 ]++;
} // end for
// print the table
System.out.printf( "%3s%12s%12s\n",
"Sum", "Frequency", "Percentage" );
// ignore subscripts 0 and 1
for ( int k = 2; k < totals.length; k++ )
{
double percent = ( double )totals[ k ] / 36000 * 100;
System.out.printf( "%3d%12d%12.2f\n", k, totals[ k ], percent );
} // end for
} // end method rollDice
} // end class Roll36``````

## All 3 Replies

You do not have a main method! I added the main method declaration and now it works just fine!

``````// Program simulates rolling two six-sided dice 36,000 times.
import java.util.Random;
public class Roll36
{
public static void main(String[] args){
Roll36 roller = new Roll36();
//I'd pass the 36000 into the rollDice method as an argument
//Or better yet, do the for loop in main and
//Have the rollDice method do nothing but roll the dice and return
//result
roller.rollDice(36000);
}
// simulate rolling of dice 36000 times
public void rollDice(int rolls)
{
Random randomNumbers = new Random();
int face1; // number on first die
int face2; // number on second die
int totals[] = new int[ 13 ]; // frequencies of the sums
// initialize totals to zero
for ( int index = 0; index < totals.length; index++ )
totals[ index ] = 0;
// roll the dice
for ( int roll = 1; roll <= rolls; roll++ ) {
face1 = 1 + randomNumbers.nextInt( 6 );
face2 = 1 + randomNumbers.nextInt( 6 );
totals[ face1 + face2 ]++;
} // end for
// print the table
//It's common in java to use println rather than printf
System.out.println( "%3s%12s%12s\n",
"Sum", "Frequency", "Percentage" );
// ignore subscripts 0 and 1
for ( int k = 2; k < totals.length; k++ )
{
double percent = ( double )totals[ k ] / rolls * 100;
//I don't think this is going to print what you think
System.out.println( "%3d%12d%12.2f\n", k, totals[ k ], percent );
} // end for
} // end method rollDice
} // end class Roll36``````

Oh, and if you are going to program in Java... please, please put your classes in a package. And don't get out of that habit.

``````package test.daveny;
//Now the imports``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.