Results 1 to 4 of 4
  1. #1
    touandcim is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default 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


    }

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Something like this should work:
    Java Code:
    System.out.printf("%-10s%-25d%2.1f%%\n", possibleSums[counter], dieTotalCount[counter], percentDieTotals[counter]*100);
    Also, please remember to use code tags when posting code.

  3. #3
    touandcim is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. Java Program (rolling the dice)
    By genocist in forum New To Java
    Replies: 8
    Last Post: 03-01-2010, 03:15 PM
  2. beehive simulation
    By BlueF4re in forum New To Java
    Replies: 2
    Last Post: 12-02-2009, 08:31 AM
  3. Small Dice Program
    By kimmelim in forum New To Java
    Replies: 13
    Last Post: 02-15-2009, 01:01 AM
  4. Mars Simulation Project 2.84
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 06-26-2008, 06:18 PM
  5. Pulley Simulation..HELP
    By dazza-s in forum Java 2D
    Replies: 2
    Last Post: 06-18-2008, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •