I have made a program to take x number of die and rolls them y number of times, then stores the data into an array so that I may output a CSV file. Everything works as intended, but I am having trouble figuring out how to increase the number of die to anything substantial. Right now I am using a switch, but linearly adding code like this seems inefficient, not to mention it will crash with amounts larger than 4 die. Is there some shortcut for adding variable number of switch statements? Any other methods would work as well, I am just not clever enough to come up with any as of yet.
import javax.swing.JOptionPane;
public class histogram {
public static void main(String[] M83cluster) {
// # of die
String N = JOptionPane.showInputDialog("How many dice would you like to roll?");
int numofDie = Integer.parseInt(N);
// # of rolls
String M = JOptionPane.showInputDialog("how many times would you like to roll?");
int numofRolls = Integer.parseInt(M);
int maxValue = numofDie*6;
int[] taco = new int[maxValue]; // for every die there will be at most 6 values.
// rolls the die and obtains a value.
for (int i=0;i<numofRolls; i++) {
int oneTotalRoll = 0;
for (int k=0;k<numofDie; k++) {
oneTotalRoll += (int)(1+6*Math.random());
}
//int oneTotalRoll = (int) (valueofDice * numofDie);
System.out.println("ROLL: " + oneTotalRoll);
// for each roll, increment taco[] array.
switch (oneTotalRoll) {
case 4: taco[0] += 1;
break;
case 5: taco[1] += 1;
break;
case 6: taco[2] += 1;
break;
case 7: taco[3] += 1;
break;
case 8: taco[4] += 1;
break;
case 9: taco[5] += 1;
break;
case 10: taco[6] += 1;
break;
case 11: taco[7] += 1;
break;
case 12: taco[8] += 1;
break;
case 13: taco[9] += 1;
break;
case 14: taco[10] += 1;
break;
case 15: taco[11] += 1;
break;
case 16: taco[12] += 1;
break;
case 17: taco[13] += 1;
break;
case 18: taco[14] += 1;
break;
case 19: taco[15] += 1;
break;
case 20: taco[16] += 1;
break;
case 21: taco[17] += 1;
break;
case 22: taco[18] += 1;
break;
case 23: taco[19] += 1;
break;
case 24: taco[20] += 1;
break;
case 25: taco[21] += 1;
break;
}
}
System.out.println("-------");
String gorgon = null; // prints outcome
for (int g=0; g<maxValue ; g++) {
String gigabolt = (taco[g] + ",");
gorgon += gigabolt;
// System.out.print(gigabolt);
}
if (gorgon.endsWith(","))
gorgon = gorgon.substring(4, gorgon.length() - 1);
System.out.print(gorgon);
}
}