Search code examples
javarandomdice

Two die simulator do-while-if-else-if error


This is the assignment that was give to me. But I can't seem to understand what is wrong with my program and how to go about fixing it. It just keeps rolling dice non-stop and freezes my JCreator. I even tried changing the NUMBER value to 10 and it still does the same thing.

  1. I have declared all the variables. You need to add code to simulate rolling the dice and keeping track of the doubles. Convert the algorithm below to Java and place it in the main method after the variable declarations, but before the output statements. You will be using several control structures: a while loop and an if-else- if statement nested inside another if statement. Use the indenting of the algorithm to help you decide what is included in the loop, what is included in the if statement, and what is included in the nested if-else-if statement.
  2. To “roll” the dice, use the nextInt method of the random number generator to generate an integer from 1 to 6.

Solution

  • You are not using a doop loop correctly. You have a do-loop and while loop, not a single do-loop. In the do-loop count never increases so the loop will never end. A do loop performs the first iteration before evaluating whether to continue.

    import java.util.Random;
    
    public class DiceSimulation
    {
        public static void main(String[] args)
        {
            final int NUMBER = 10000;
    
            Random generator = new Random();
    
            int die1Value;
            int die2Value;
            int count = 0;
            int snakeEyes = 0;
            int twos = 0;
            int threes = 0;
            int fours = 0;
            int fives = 0;
            int sixes = 0;
    
            do{
                die1Value = generator.nextInt(6) + 1;
                System.out.println("You rolled: " + die1Value);
    
                die2Value = generator.nextInt(6) + 1;
                System.out.println("You rolled: " + die2Value);
    
                if (die1Value == die2Value)
                { 
                    if(die1Value == 1)
                    {
                        snakeEyes++;
                    } 
                    else if (die1Value == 2)
                    {
                        twos++;
                    } 
                    else if (die1Value == 3)
                    {
                        threes++;
                    } 
                    else if (die1Value == 4)
                    {
                        fours++;
                    } 
                    else if (die1Value == 5)
                    {
                        fives++;
                    } 
                    else if (die1Value == 6)
                    {
                        sixes++;
                    }
                }
                count++;
            }while (count < NUMBER);
    
            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.");
        }
    }