How can I get my dice roller to completely loop and sort results correctly?

So I have this code that I wrote and for now it only works with two dice. I am currently trying to get it to recognize more than two dice or possibly one die but for now I keep getting exceptions in the program. I know why but I'm not sure how to fix it. The only things I can say for sure if that I need to make my array size up properly with the number of dice I'm throwing like and also fits the results of the dice thrown since it needs to keep track of how many times a certain number came out depending on how many dice were thrown, how many sides they had, and how many times they were thrown. Can anyone please help me with this? Thank you.

Code :

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dice.roller;
/**
*
* @author Marlin
*/
import java.util.*;
public class DiceRoller {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String name;
int roll, sides, dice, add = 0, tmp = 0;
int[] result;
//temp
result = new int[11];
System.out.println("Hello, what is your name?");
name = keyboard.next();
System.out.println("Hello, " + name + ", how are you today?");
System.out.println("How many times would you like to roll the dice?");
roll = keyboard.nextInt();
while (tmp < 11) {
result[tmp] = 0;
tmp++;
}
System.out.println("You want to roll the dice " + roll + " times.");
System.out.println("How many dice would you like to roll?");
dice = keyboard.nextInt();
System.out.println("You would like to throw " + dice + " dice.");
for (int i = 0; i < roll; i++) {
add = 0;
for (int x = 0; x < dice; x++) {
int die1 = (int) (Math.random() * 6) + 1;
// int die2 = (int)(Math.random()*6) + 1;
// add = die1 + die2;
add += die1;
}
//System.out.println(add);
++result[add - 2];
}
tmp = 0;
while (tmp < 11) {
int counter = tmp + 2;
System.out.println("The result " + counter + " came out " + result[tmp] + " times");
tmp++;
}
}
}

Re: How can I get my dice roller to completely loop and sort results correctly?

Quote:

Originally Posted by

**Skynet928**
...more than two dice ...how many sides...]

Look at your program, and think back to the time when you were designing it.

What statements in the program depend on the number of dice being two?

What statements in the program depend on each die having six sides?

You already have variables "dice" and "sides" that you should be able to incorporate into the program in places where they make a difference. For this discussion, I'll call them *N* and *S*.

Well, one part is easy: Change the random number call to let the value of a single die be *1, 2, ... , S* rather than *1, 2, ..., 6* that you have now.

Next:

What is the range of values that you can get if you toss *N* of those dice? That will determine the size that the *result* array should be.

Lowest value of a single play is *N*, highest is *N*S*

The easiest way to accommodate this range of sums might be to declare an array with *N*S+1* elements, knowing that *0, 1, ..., N-1* won't be used, and the sums will be held in *N, N+1, ... , N*S*

Then, for each play, you simply add up the *N* dice values and store the sum in the array element with index equal to the sum. If the sum is, say 8, store it in *result[8]*.

After a play, if you want to know and report, say, the number of sevens that were thrown, just look at *result[7].*

If you don't want to have the useless lower values, then you should be able to use your present approach and figure out what the size of the array will be and figure out how the index depends on the sum so that it will be stored in the correct array member. (Same approach to associating an index value to retrieve a given sum.)

I mean, look at the "magic numbers" in statements like the following that depend on two six-sided dice). For example:

Code java:

result = new int[11];
while (tmp < 11) {
++result[add - 2];
.
int counter = tmp + 2;

See what I mean? Where did the numbers *11* and *2* come from?

They depend on the number of dice and the number of sides of each die.

Start by changing the constants *2* and *11* to *expressions* involving your *dice* and *sides* variables such that they work for 2 six-sided dice. Then try with three six-sided dice. (Set dice equal to 2 and sides equal to 6.)

Now, don't just start plugging stuff into the program and see if you get lucky. Work it out on pencil and paper to make sure the expressions make sense for different numbers of dice and different numbers of sides, then test. Maybe put some print statements in the loop that shows the individual die values and the sum. That might help you pull things together:

Code java:

for (int i = 0; i < roll; i++) {
sum = 0;
for (int x = 0; x < dice; x++) {
//
// Change the next line to take into account the number of sides
int die1 = (int) (Math.random() * ???) + 1;
System.out.print(die1 + " "); // For debugging
sum += die1;
}
System.out.println("--> " + sum); // For debugging
// Put code here to increment the appropriate element of result
++result[what???]
}

Test with two six-sided dice. Test a lot. Make sure it does and reports exactly what you expect.

Change it to test with three six-sided dice. Test a lot.

Then, change to, say, two 12-sided dice and see how the values of the expressions change. Test.

Etc.

Cheers!

Z