0

I have the following code, with the purpose of rolling a dice 10,000 times and then adding ALL of the pair of die rolls:

 package assignments;

    import java.util.Random;

    public class Dice {

        private int numbers;
        private static Random generator;

        public Dice() {
            generator = new Random();
            numbers = 0;    
        }

        public void Roll() {
            numbers = generator.nextInt(6) + 1;
        }

        public int getNumbers() {
            return numbers;
        }

    }

and also:

package assignments;

public class RollDice {

    public static void main(String[] args) {

        final int rollCount = 10000;

        Dice die1 = new Dice();
        Dice die2 = new Dice();

        int die1Number;
        int die2Number;

        int count = 0;

        int dieTotal = 0;

        for (count = 0; count < 10000; count++) {
            die1.Roll();
            die1Number = die1.getNumbers();

            die2.Roll();
            die2Number = die2.getNumbers();

            dieTotal = die1Number + die2Number;
            count++;
        }

        System.out.println(dieTotal);
    }
}

The problem is that either the die is not actually being rolled 10,000 times, or it is simply making a new input for the die every time the loop goes through.

Can someone please help me add the new die rolls every time?

3
  • Never mind got it. Any suggestions any ways? Commented Mar 16, 2015 at 2:14
  • Beyond what I mentioned in my answer, you should see CokaCola's comment, as this logic error of incrementing count twice at each iteration will provide incorrect results. Commented Mar 16, 2015 at 2:17
  • It may be helpful if you posted expected output, actual output, and any errors you are getting Commented Mar 16, 2015 at 2:18

3 Answers 3

1

i improved your second class code. removed redundant code

package assignments;

public class RollDice {

    public static void main(String[] args) {

        final int rollCount = 10000;

        Dice die1 = new Dice();
        Dice die2 = new Dice();

        int die1Number;
        int die2Number;

        int dieTotal = 0;

        for (int count = 0; count < rollCount ; count++) {
            die1.Roll();
            die1Number = die1.getNumbers();

            die2.Roll();
            die2Number = die2.getNumbers();

            dieTotal += die1Number + die2Number; 
        }

        System.out.println(dieTotal);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your dieTotal is overwritten at each loop iteration and will not hold the sum of all rolls at the end of the loop, but rather the sum of the last iteration.
@Shashu, pls Click the tick (check mark) on left under the vote arrows if it helps for others to easily find the answer. tnx
0

You're overwriting the value for the dieTotal variable at every iteration by using the = operator. Use dieTotal += (die1Number + die2Number), this is the same as saying dieTotal = dieTotal + die1Number + die2Number, which will add the old value of dieTotal as well as the new die rolls.

Comments

0

You're adding count++ to the bottom of your for loop.

For loops will count automatically, so by doing this, you're effectively halving the number it counts to, making it 5,000 instead of 10,000. You also don't need to specify int count = 0; outside the loop.

you can remove those and simply use for(int count = 0; count < 10000; count++).

You will also want to check Jame's, as he said, do dieTotal += (die1Number + die2Number); or something instead.

1 Comment

For loops don't do anything "automatically", they do it because it is written in the for loop header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.