So im just starting learrning code, and I am also using code.org as a starter tool. I'm on a lesson where it says to;

write a program that simulates the rolling of two dice. Keep rolling the dice UNTIL the sum of the dice is either a 7 OR an 11.

And I am having very much trouble for figuring out how to do that. Again, I am very new to coding.

Dani AI

Generated

The task is to simulate rolling two six-sided dice repeatedly and stop when the sum is 7 or 11. ’s reminder to follow the lessons is sound — the key is basic loop control — and ’s loop hint is on the right track. The Java example below is a complete, minimal implementation and a few troubleshooting notes follow.

import java.util.concurrent.ThreadLocalRandom;

public class RollDiceUntil7or11 {
    public static void main(String[] args) {
        int die1, die2, sum;
        do {
            die1 = ThreadLocalRandom.current().nextInt(1, 7); // 1..6
            die2 = ThreadLocalRandom.current().nextInt(1, 7);
            sum = die1 + die2;
            System.out.printf("Rolled %d + %d = %d%n", die1, die2, sum);
        } while (sum != 7 && sum != 11);
        System.out.println("Stopped — sum is " + sum);
    }
}

Notes and common pitfalls:

  • Generating 1–6: nextInt(1, 7) gives 1..6; nextInt(6) + 1 is an alternate. Off-by-one errors here change outcomes.
  • Loop logic: continuing while the sum is neither 7 nor 11 uses sum != 7 && sum != 11. Using || instead creates a condition that is always true and causes an infinite loop.
  • Use a do/while when at least one roll must occur. A while loop can work too but needs an initial roll before the loop.
  • For debugging, print each roll. For reproducible testing, use a Random with a fixed seed instead of the default seeding.

If working in code.org’s block or JavaScript environments, keep the same algorithm: generate two random integers 1–6, add them, and stop when the sum equals 7 or 11. This preserves the logic without changing the core approach.

Recommended Answers

All 2 Replies

The most common source of this issue is skipping the lessons. Go back through the tutorials since good classes and tutorials build up to the assignment. Skip a chapter and it's like an Ikea kit missing a component.

Here's some pseudo-code to point you in the right direction

do
    get two random numbers 1-6
    sum  them together
while( the sum is not 7 or 11)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.