Pregunta

import java.util.Scanner;

public class DiceSimulator {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);    
      System.out.print("How many dice do you want to roll: ");
      int amountOfDie = input.nextInt();
      System.out.println("");

      // declare diceArray    
      Die[] diceArray = new Die[amountOfDie];

      // create a new Die for each reference    
      int maxRoll = 0;
      for (int i = 0; i < diceArray.length; i++) {   
         System.out.print("How many sides on die number " + (i + 1) + "" + ": ");
         int numSides = input.nextInt();
         diceArray[i] = new Die(numSides);
         int minRoll = amountOfDie;
         maxRoll += numSides;
      }
      int minRoll = amountOfDie;

      // int[] sumArray = new int[maxRoll + 1];//to store sum

      System.out.println("");
      System.out.print("How many times do you want to roll: ");
      int numRol = input.nextInt();

      System.out.println("\nResults");

      // ******** right here is where I'm having the issue.

      for (int i = 0; i < numRol; i++) {
         diceArray[i].roll();
         // System.out.println(diceArray[i]);
         int sum = 0;
             for (int f = 0; f < numRol; f++) {
                  sum += diceArray[f].roll();
                  int[] sumArray = new int[maxRoll + 1];
                  sumArray[sum]++;
                  System.out.print("\t" + sum);
         }
      }
      // for(Die d: diceArray){
      // System.out.println(d);
      // }
   }
}

See commented line in code: // ******** right here is where I'm having the issue.

The for loop should spit out the sum of the rolls.
It just isn't spitting out the right values. I'm just curious where I went wrong? The program should ask the user how many rolls. This would go into the first for loop and the second would roll the dice that many times.

¿Fue útil?

Solución

I think what you're trying to do is roll these dice a said number of times, keeping track of how often each total is reached, so you can print them at the end. Try this:

int[] sumArray = new int[maxRoll];
for (int i = 0; i < numRol; i++) {
    int sum = 0;
    for (Die d : diceArray) {
        int roll = d.roll();
        sum += roll;
        System.out.print("\t" + roll);
    }
    System.out.println("\t:\t" + sum);
    sumArray[sum-1]++;
}
for (int i  = 0; i < sumArray.length; i++){
    System.out.printf("%d: %d Rolls\n", i+1, sumArray[i]);
}

You can see it working here. Your most basic mistakes were:

  1. Declare your sum array before you start calculating sums.
  2. In the inner loop, iterate over your dice, one at a time, rolling, adding to the sum, and printing.
  3. Print your sum and increment your count after the dice have been rolled.

If you roll two six sided dice 10 times with this algorithm, you'll get:

Results
    4   3   :   7
    5   5   :   10
    2   2   :   4
    6   5   :   11
    1   1   :   2
    6   5   :   11
    6   5   :   11
    1   2   :   3
    2   1   :   3
    3   5   :   8
1: 0 Rolls
2: 1 Rolls
3: 2 Rolls
4: 1 Rolls
5: 0 Rolls
6: 0 Rolls
7: 1 Rolls
8: 1 Rolls
9: 0 Rolls
10: 1 Rolls
11: 3 Rolls
12: 0 Rolls

Otros consejos

You have a line that looks like this:

diceArray[i].roll();

The problem is that diceArray is only large as the number of dice you have. But you are trying to use it with an index of the number of rolls. This could cause an ArrayOutOfBoundsException.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top