Hi everybody. Sorry for the newbie post. I have an assignment I was just looking for some help with. I need to create a dice game that generates a random two dice throw and then gives you three tries to guess the number. I seem to be a bit stuck. I can't figure out whether to put the random generator inside the "while" loop or not. Any help would be greatly appreciated. Thanks in advance!

import javax.swing.JOptionPane;
 import java.util.Random;

public class dice2
    public static void main(String[] args)
        int guessOne;
        int guessTwo;
        int roll = 0;
        String userInput;
        String userInput2;

        userInput = JOptionPane.showInputDialog(null, "Please guess a number between 2 and 12.");
        guessOne   = Integer.parseInt(userInput);

        if (guessOne > 12 || guessOne < 2)

                 userInput2 = JOptionPane.showInputDialog("Please make sure your guess is between 2 and 12.");
                 guessOne   = Integer.parseInt(userInput2);
            {   JOptionPane.showMessageDialog(null, "Ok...time to roll!");

                Random rollOne = new Random();
                int nu = 1+ rollOne.nextInt(12);
                    while (guessOne != nu || roll<3)
                    roll = roll + 1;

                    JOptionPane.showMessageDialog(null, "You rolled a " + nu);
                    JOptionPane.showMessageDialog(null, "Sorry...try again");




Sorry I didn't explain it correctly. The game will allow you to guess a number once, and then you get three "shakes" to see if you can roll that number.

You will go round the while loop up to 3 times, and you need a new roll of the dice each time, so thenu = 1+ rollOne.nextInt(12); must be insde the loop.

ps: that line is not an accurate model of rolling two dice. There's only one way to roll a 2 (ie two ones) and only one way to roll a 12, but there are multiple ways to roll a 7 (1+6, 2+5, 3+4). (and there are no ways roll a 1). You can get the correct answer by using the results of two calls to nextInt(6)

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.