0

I have to convert it to using for loop from while loop. I don't know how to start with it :/

import java.util.Random;        //to use the random number generator 
        public class DiceSimulation
        {
            public static void main(String[] args)
            {
                final int NUMBER = 10000;   //the number of times to roll the dice

                //a random number generator used in simulating rolling a dice
                Random generator = new Random();

                int die1Value;          // number of spots on the first die
                int die2Value;          // number of spots on the second die
                int count = 0;          // number of times the dice were rolled
                int snakeEyes = 0;      // number of times snake eyes is rolled
                int twos = 0;           // number of times double two is rolled
                int threes = 0;     // number of times double three is rolled
                int fours = 0;          // number of times double four is rolled
                int fives = 0;          // number of times double five is rolled
                int sixes = 0;          // number of times double six is rolled

                //ENTER YOUR CODE FOR THE ALGORITHM HERE
                while(count < NUMBER)
                {
                    die1Value = generator.nextInt(6) + 1;   //returns 1,2,3,4,5,or 6
                    die2Value = generator.nextInt(6) + 1;   //returns 1,2,3,4,5,or 6

                    if(die1Value == die2Value)
                    {
                        if (die1Value == 1)
                            snakeEyes++;
                        else if (die1Value == 2)
                            twos++;
                        // Task #1 step 3: To do - code for die1Value = 3, 4, 5, and 6
                                        else if (die1Value == 3)
                                            threes++;
                                        else if (die1Value == 4)
                                            fours++;
                                        else if (die1Value == 5)
                                            fives++;
                                        else if (die1Value == 6)
                                            sixes++;
                    }
                    count++;
                }

                System.out.println ("You rolled snake eyes " + snakeEyes +
                    " out of " + count + " rolls.");
                System.out.println ("You rolled double twos " + twos +
                    " out of " + count + " rolls.");
                System.out.println ("You rolled double threes " + threes +
                    " out of " + count + " rolls.");
                System.out.println ("You rolled double fours " + fours +
                    " out of " + count + " rolls.");
                System.out.println ("You rolled double fives " + fives +
                    " out of " + count + " rolls.");
                System.out.println ("You rolled double sixes " + sixes +
                    " out of " + count + " rolls.");
            }
        }
2
Contributors
1
Reply
12
Views
4 Years
Discussion Span
Last Post by JamesCherrill
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.