I am rolling dice (two die) n number of times. From the n number of rolls, I need to count how many times I got each each occurrence.
The possibilities of two dies rolling is a sum of 2 to 12.
I made an ArrayList of 13 elements.
Now I have to find a way to add each possibility into the ArrayList.
With the ArrayList I will not need 11 if statements.
How do I add each possibility to the ArrayList.
I am using a Dice class I made, everyhing is compiling in the Applet.
My Code:
CODE
import java.util.*;//array list
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/**
* Class DiceRoll - write a description of the class here
*
* @author (your name)
* @version (6a-4)
*/
public class DiceRoll extends JApplet
implements ActionListener
{
private Dice dice1, dice2;
private JLabel numRollLabel, pressEnter, dice1Label, dice2Label;
private JTextField numRollField, field2;
private JPanel rollPanel, countPanel;
private ArrayList <Integer> sumList;
private int sum=0;
public void init()
{
//********north**********************
rollPanel = new JPanel();
numRollLabel = new JLabel("Number of Rolls");
numRollField = new JTextField("", 10);
numRollField.addActionListener(this);
pressEnter = new JLabel("Press Enter When Ready");
pressEnter.setForeground(Color.pink);
dice1 = new Dice();
dice2 = new Dice();
dice1Label = new JLabel(dice1.getPic());
dice2Label = new JLabel(dice2.getPic());
rollPanel.setLayout(new FlowLayout());
rollPanel.add(numRollLabel);
rollPanel.add(numRollField);
rollPanel.add(pressEnter);
rollPanel.add(dice1Label);
rollPanel.add(dice2Label);
//*************South*****************
countPanel = new JPanel();
field2 = new JTextField("",3);
countPanel.setLayout(new FlowLayout());
countPanel.add(field2);
Container window = getContentPane();
window.setLayout(new BorderLayout ());
window.add(rollPanel, BorderLayout.NORTH);
window.add(countPanel, BorderLayout.SOUTH);
sumList = new ArrayList<Integer>();
for(int i=0; i<=13; i++)
{
sumList.add(0);
}
}
public void actionPerformed( ActionEvent ae)
{
if(ae.getSource()==numRollField)
{
roll();
}
}
public void roll()
{
int x, rolls;
int count2=0;
rolls = stringToInt(numRollField.getText());
for(x = 0; x<=rolls; x++)
{
dice1.roll();
dice2.roll();
dice1Label.setIcon(dice1.getPic());
dice2Label.setIcon(dice2.getPic());
sum = dice1.getRoll()+ dice2.getRoll();
}
field2.setText(""+sum);
numRollField.setText("");//last action
}
private int stringToInt(String strObj)
{
return Integer.parseInt(strObj.trim());
}
}