1
\$\begingroup\$

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("");
    }




}
}
\$\endgroup\$
2
  • \$\begingroup\$ This would be easier to review if we had the 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\$
    – Brythan
    Commented Nov 4, 2014 at 10:52
  • 1
    \$\begingroup\$ 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\$
    – Rotem
    Commented Nov 4, 2014 at 12:29

2 Answers 2

5
\$\begingroup\$

Some points:

  • Class names should start with a captical. So do not use myDice as class name but MyDice
  • Remove unnecessary whitespace (you have multiple lines, that makes code header to read)
  • Fix the indentation of the { and } as they are inconsistent
  • Please edit the post and add your MyDice class.
  • Add comments to tell WHY you are doing thing in your code (what is the role of n?)
  • I would make a method that checks if the dice[] are all equal

  • You initialize the dice array each iteration. If the setRoll() adjusts the roll, then you can re-use the array, and initialize it outsize the main for loop

  • And you should check the dice only after rolling them all...
\$\endgroup\$
4
\$\begingroup\$

Comments

You have some comments which don't tell the reader anything they didn't already know: creates an array of 6 dice, creates a scanner, fills each slot with dice. You can assume that the reader will be at least somewhat familiar with the language itself.

What you should comment on is why you do things, and if it is unclear, what.

Misc

  • assign values to variables on the same line as you declare them: int n = myScan.nextInt();
  • be consistent with your spaces (use an IDE, it will format your code for you)
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.