简体   繁体   中英

Rolling m die with n sides x times

Okay so I changed my code around and deleted a lot of the unnecessary garbage in it. It works for some numbers but not for others, for example, when I put in 100 rolls/8 sides/3 die it gives me an out of bounds error despite the limits I've set for it. Obviously I've looked over some detail, I'm just not sure what detail it is.

public class Ass11f {

    public static void main(String[] args) {
        EasyReader console = new EasyReader();
        System.out.print("Enter how many times you want to roll the die: "); 
        int numRolls = console.readInt();
        System.out.print("Enter the amount of sides: ");
        int numSides = console.readInt();           
        System.out.print("Enter the amount of die: ");
        int numDie = console.readInt();     
        int[] rollSum = new int[numDie*numSides];

        for (int i = 0; i<numRolls; ++i)
            {
            int rollCounter=0;
            for (int l = 0; l<numDie; ++l){
                rollCounter += ((int)(Math.random()*numSides)+1);
            }
            rollSum[rollCounter]++;
        }     
        for (int m = 2;m<=rollSum.length;++m) System.out.println(m+"'s: "+rollSum[m]+" times, "+((((double)rollSum[m])/numRolls)*100)+"%");                                                   
    }
}

There are two base problems:

  1. When adding roll totals, you're trying to add the maximum roll in an index one past the end of the array. The easy fix is to simply add 1 to the length of your array.
  2. When printing, you cannot access an array using an index equal to the array's length, which is what m<=rollSum.length will eventually do. Replace that with m < rollSum.length so it stops before the final value.

Also, here's some ways to make your array creation a bit clearer:

    // The minimum value is always numDie.
    // The maximum is always numDie * numSides
    // There are maximum - minimum + 1 possible values (ie 6 on a d6)
    int maximum = numDie * numSides;
    int minimum = numDie;

    // Remember, index zero is now the minimum roll. 
    // The final index is the maximum roll. So the count at an index is really
    // the count for any roll with value index + minimum
    int[] rollSum = new int[maximum - minimum + 1];

I also recommend splitting up that print statement. It's a bit easier to read and debug. Also, you can start at numDie instead of 2 to account for when you have more or less die than 3:

    for (int i = numDie; i < rollSum.length; ++i) {
        // Print the first bit, ie "2's: ".
        System.out.print(i + "'s: ");

        // How many times was that value rolled?
        System.out.print(rollSum[i] + " times, ");

        // What percentage is that?
        double percentage = ((double)rollSum[i]) / numRolls * 100;
        System.out.println(percentage + "%");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM