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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with 3 guess Dice game

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Dice game need help
    By lf2killer in forum Loops & Control Statements
    Replies: 6
    Last Post: October 5th, 2012, 02:25 AM
  2. Java Programming - Dice Game Help!
    By blackvelvet in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 19th, 2012, 11:47 AM
  3. Dice game code creating help!
    By kodols in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 22nd, 2011, 07:01 PM
  4. Dice Game help and Arrays
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 9th, 2011, 10:08 PM
  5. Dice game help, a little cleanup
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 12:28 PM