Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Help with 3 guess Dice game

1. ## 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.

2. ## Re: Help with 3 guess Dice game

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.