For this lab you should re-use your Dice class from the previous week.
The user inputs a number n which determines the number of groups of 6 dice to be rolled.
The program should output x, which is the number of times 6n dice are rolled before they show an equal number of all values. For example, if n is 1, then x will be the number of times you need to roll 6 dice before you get a 1, 2, 3, 4, 5 and 6.
On average, this value is around 64.8.
Don’t calculate this mathematically, do it via a Monte Carlo simulation. The output number x will be variable because it is subject to chance. Do multiple experiments to find a stable average value for x.
{
public static void main(String [] args){
myDice[] diceArray = new myDice[6]; // creates an array of 6 dice
Scanner myScan = new Scanner(System.in); // creates a scanner
int n;
System.out.println("Enter a number : ");
n= myScan.nextInt();
for(int a=0;a<n;a++)
{
for (int i = 0; i<diceArray.length; i++)
{
diceArray[i] = new myDice(); // fills each slot with dice
}
for (int j=0; j<diceArray.length; j++) //rolls 6 dice, prints each roll
{
diceArray[j].setRoll();
int rollValue = diceArray[j].getRoll();
if (diceArray[0] == diceArray[1] &&
diceArray[1] == diceArray[2] &&
diceArray[3] == diceArray[4] &&
diceArray[4] == diceArray[5])
{
System.out.print(rollValue);
}
else{ System.out.print(rollValue);
n++; }
}
System.out.println("");
}
}
}
myDice
class. It would also be easier if it were more consistently formatted. As a general rule, you should either use spaces or use tabs, not both at once. You should give the results of your testing, as it looks like your program has a bug. You should probably get it working before asking for a review. \$\endgroup\$System.out.print(rollValue);
is called in both clauses of the if/else statement. Call it once, either before or after the if/else. \$\endgroup\$