# Help with 3 guess Dice game

• November 28th, 2012, 05:50 PM
tabmanmatt
Help with 3 guess Dice game
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);
}
else
{ 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");
}
}
}
}

--- Update ---

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.
• November 28th, 2012, 07:42 PM
Norm
Re: Help with 3 guess Dice game
Quote:

I can't figure out whether to put the random generator inside the "while" loop or not
When do you want the random number? Every time around the loop or just once before the loop.
Inside the loop will change it every time.