The code I have below asks the user how many times you want to roll the dice. It rolls dice 1 and dice 2 randomly and gives the total. My programming is all good to this point.
Now, I'm trying to keep track of the result of each roll using an array that is indexed by the sum of the roll of the two dice. Then I want to output my result in a table that shows each value (from 2 - 12)) and the number of times that value was rolled. I would like to do this preferably with the JTextArea class, but it doesn't have to be. I keep getting errors. Can anyone help? The code below works for the dice rolling in the 1st paragraph. I took out all the bad code I was trying to use for the 2nd paragraph.
package Part2pack;
import java.util.Random;
import javax.swing.JOptionPane;
class Dice{
public static void main (String args[])
{
String input = " ";
int count = 0,dice1,dice2;
input = JOptionPane.showInputDialog(null, " Enter how many times the dice should be rolled: ");
count = Integer.parseInt(input);
Random random = new Random();
for (int num = 1; num <= count; ++num)
{
dice1=dice2=0;
dice1=getRandomInteger(1, 6, random);
dice2=getRandomInteger(1, 6, random);
System.out.println("Roll#");
System.out.println(num);
System.out.println("1st dice");
System.out.println(dice1);
System.out.println("2nd Dice");
System.out.println(dice2);
System.out.println("Total:");
System.out.println(dice1+dice2);
}
}
private static int getRandomInteger(int begin, int finish, Random random)
{
long range = (long)finish - (long)begin + 1;
long fraction = (long)(range * random.nextDouble());
int randomNumber = (int)(fraction + begin);
return randomNumber;
}
}