Results 1 to 4 of 4
Thread: Dice Simulation Program
- 03-14-2010, 05:36 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
Dice Simulation Program
So I'm not gonna lie, I need help with homework. I basically have to write a program that simulates the rolling of two die 100000 times. I achieved this using a for loop. Each time a sum comes up (2, 3, 4, 5....12) I increment the corresponding array element (2 corresponds to dieTotalCount[0] and 3 corresponds to dieTotalCount[1] etc.). I then calculate the percentage and of each total (out of 100000) and display the sum values, total times they are rolled and the percentage of times they are rolled out of 100000. I have the whole program written. My problem comes when I am trying to format the percentage(float type). It doesn't show the percentages at all, just the %2.1f for every damn line. If anyone could help, that'd be awesome! Oh yeah. this is the code:
package vangasg9;
/**
*
* @author Tou Vang
*/
import java.util.Random;
public class VangAsg9Dice {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Random randomNumbers = new Random();
int dieOne;
int dieTwo;
int dieSum = 0;
int[] possibleSums = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int[] dieTotalCount = new int[11];
double[] percentDieTotals = new double[11];
double numberOfRolls = 100000;
//rolls two die 100000 times and totals them each time
//stores them into dieTotalCount array
for (int counter = 1; counter <= 100000; counter ++){
dieOne = 1 + randomNumbers.nextInt(6);
dieTwo = 1 + randomNumbers.nextInt(6);
dieSum = dieOne + dieTwo;
switch (dieSum){
case 2:
++dieTotalCount[0];
break;
case 3:
++dieTotalCount[1];
break;
case 4:
++dieTotalCount[2];
break;
case 5:
++dieTotalCount[3];
break;
case 6:
++dieTotalCount[4];
break;
case 7:
++dieTotalCount[5];
break;
case 8:
++dieTotalCount[6];
break;
case 9:
++dieTotalCount[7];
break;
case 10:
++dieTotalCount[8];
break;
case 11:
++dieTotalCount[9];
break;
case 12:
++dieTotalCount[10];
}//end switch
dieOne = 0;
dieTwo = 0;
dieSum = 0;
}//end for
//calculates the percentage of roll totals for the 11 numbers
for(int counter = 0; counter <= 10; counter++){
percentDieTotals[counter] = (double)dieTotalCount[counter]/numberOfRolls;
}//end for
//output
System.out.println("This is a summary of the sums of two die rolled 100,000 times.\n\n");
System.out.printf("%-10s%-25s%20s", "Sum:", "Number of times rolled:", "Percentage of total:\n");//displays headers
//displays numbers
for(int counter = 0; counter <= 10; counter++){
System.out.printf("%-10s%-25d%25%2.1f\n", possibleSums[counter], dieTotalCount[counter], percentDieTotals[counter]);
}[/SIZE][/SIZE]
}//end main
}
- 03-14-2010, 06:24 PM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Something like this should work:
Also, please remember to use code tags when posting code.Java Code:System.out.printf("%-10s%-25d%2.1f%%\n", possibleSums[counter], dieTotalCount[counter], percentDieTotals[counter]*100);
- 03-15-2010, 06:52 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
thanks a bunch Collin. I had figured out what I was doing wrong already. It's funny how little things like formatting make such a big difference. Sorry bout not using the code tags. I will remember that in the future!
thanks again man.
-
If you use an array if int[13] and ignore the [0] and [1] position, you can simply things greatly including getting rid of that switch statement. Collin's code seems to have disappeared. ???
Similar Threads
-
Java Program (rolling the dice)
By genocist in forum New To JavaReplies: 8Last Post: 03-01-2010, 03:15 PM -
beehive simulation
By BlueF4re in forum New To JavaReplies: 2Last Post: 12-02-2009, 08:31 AM -
Small Dice Program
By kimmelim in forum New To JavaReplies: 13Last Post: 02-15-2009, 01:01 AM -
Mars Simulation Project 2.84
By Java Tip in forum Java SoftwareReplies: 0Last Post: 06-26-2008, 06:18 PM -
Pulley Simulation..HELP
By dazza-s in forum Java 2DReplies: 2Last Post: 06-18-2008, 10:51 PM


LinkBack URL
About LinkBacks

Bookmarks