0
package HomePlace;

import java.util.Scanner;
import java.util.Random;

public class DiceRolls {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("How many sides? ");
        int Usides = input.nextInt();
        System.out.print("How many dice to roll? ");
        int Udices = input.nextInt();
        System.out.print("How many rolls? ");
        int Urolls = input.nextInt();
        int[] outcomes = new int[Usides * Udices];


        for (int rolls = 0; rolls < Urolls; rolls++) {
            outcomes[sum(Usides, Udices)] += 1;
        }
        for (int i = Udices; i <= Urolls; i++) {
            System.out.println(i + ": " + outcomes[i-1]);
        }

    }

    public static int sum(int Us, int Ud) {
        int dicesum = 0;
        for (int i = 0; i <= Ud; i++) {
            int dots = (int) (Math.random() * Us + 1);
            dicesum += dots;

        }
        return dicesum;

    }

}

This is a code where the User declares how many sides (of the dice), how many dice (number of dice), and how many rolls that the User would perform. For example, if the User choose the dice would have 3 sides, the variable would be 1~3 and If the User inputs 5 dices and 5 rolls, it would be throwing 5 (3sided) dice for 5 times. My output should display the possible sums and how many times that sum came out. For example, If I throw 2 of 6 sided dice 2 times, the output should count the number of sums that came out from each roll.

Now my problem is I get the error

    How many sides? 3
How many dice to roll? 4
How many rolls? 5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 12 out of bounds for length 12
    at HomePlace.DiceRolls.main(DiceRolls.java:21)

and if I put large input

How many sides? 123
How many dice to roll? 123
How many rolls? 123
123: 0

I only get this output. where I want the output would be

How many sides on the die? 7

How many dice to roll? 5

How many rolls? 100

5: 0

6: 0

7: 0

8: 0

9: 1

10: 1

11: 2

12: 0

13: 2

14: 5

15: 9

16: 5

17: 4

18: 2

19: 9

20: 6

21: 8

22: 11

23: 10

24: 7

25: 6

26: 5

27: 2

28: 1

29: 3

30: 1

31: 0

32: 0

33: 0

34: 0

35: 0

Obviously I did something wrong in that code.. can i get help?

LogicNAME
  • 1
  • 1

1 Answers1

0

Using the first sample from your question as an example, i.e. four dice where each dice has three sides and you roll all four dice a total of five times.

The minimum outcome will be 4 when each dice lands on 1 (one), i.e. the minimum outcome equals the total number of dice.

The maximum outcome will be 12 when each dice lands on 3 (three), i.e. the maximum outcome is the number of dice multiplied by the number of sides on each dice.

Array outcomes stores the frequency of each outcome. So the size of the array needs to be the number of possible outcomes. Using the above example, the size needs to be nine, i.e. 12 - 4 + 1 or in other words the maximum outcome minus the minimum outcome plus one.

The first for loop in method main() performs the dice rolls and adjusts the values in array outcomes. You need to adjust the actual outcome to the array index. Again using the above example, if the outcome is the minimum, i.e. 4, then that corresponds to the first element in outcomes, so you need to change 4 to 0 (zero). Similarly, when the outcome is 5, you want to update the second element in outcomes, so you need to change 5 to 1 (one). As you can see, in both cases you need to subtract the minimum outcome from the actual outcome you got in order to get the real index of the element in outcomes that needs to be incremented.

The for loop in method sum() needs to repeat according to the number of dice. So for four dice, the loop needs to repeat four times. So you can write the loop as:

for (int i = 1; i <= Ud; i++) {

Finally you print the results. You want to display every element in outcomes but when you display the element value, you need to add the minimum outcome to the element index because, as I wrote above, element 0 (zero) in array outcomes really means the minimum outcome.

Here is the code. I added more print() methods so you can see what the code is doing.

import java.util.Scanner;

public class DiceRolls {
    public static int sum(int Us, int Ud) {
        int dicesum = 0;
        for (int i = 1; i <= Ud; i++) {
            int dots = (int) (Math.random() * Us + 1);
            System.out.printf("dice %d landed on %d%n", i, dots);
            dicesum += dots;
        }
        return dicesum;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("How many sides? ");
        int Usides = input.nextInt();
        System.out.print("How many dice to roll? ");
        int Udices = input.nextInt();
        System.out.print("How many rolls? ");
        int Urolls = input.nextInt();
        int maximumOutcome = Udices * Usides;
        int[] outcomes = new int[maximumOutcome - Udices + 1];
        System.out.printf("Outcomes for %d rolls of %d, %d-sided dice.%n", Urolls, Udices, Usides);
        for (int rolls = 1; rolls <= Urolls; rolls++) {
            System.out.printf("%nRoll %d:%n", rolls);
            int sum = sum(Usides, Udices);
            System.out.printf("Total: %d%n", sum);
            outcomes[sum - Udices]++;
        }
        System.out.println("====================================================================");
        System.out.println("Outcomes: ");
        for (int i = 0; i < outcomes.length; i++) {
            System.out.println((i + Udices) + ": " + outcomes[i]);
        }
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41