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 3 of 3

Thread: Die Rolling Program

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Die Rolling Program

    So first I'm supposed to create a Die class for generating a random number from 1 to 6

    import java.util.*;
    public class Die
    {
    int faceValue;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;



    public int getRoll()
    {
    return faceValue;
    }

    public void setRoll(int rolled)
    {
    faceValue = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
    }


    }




    Then my next class prints out the value of rolling two dice.


    public class TwoDice
    {
    public static void main(String[] args)
    {
    Die dieRoll = new Die();
    System.out.println("The first die roll is " + dieRoll.getRoll());
    System.out.println("The seoncd die roll is " + dieRoll.getRoll());
    }
    }


    The number that keeps printing no matter how many runs is always 0.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Die Rolling Program

    A lot of this doesn't make sense. Why does the setRoll() method take a parameter that it never uses? When is that method ever called?

    I suggest stepping through this in your head and figuring out why this doesn't do what you expect it to.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Olympaphibian89 (October 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Die Rolling Program

    Ah, I see it now. Yes, I forgot to actually call the into play the set method. I'm just jumping back into programming after a about a 2 year break so I'm super rusty. It runs fine now, tyvm.

Similar Threads

  1. Need help coding Dice rolling simulation
    By DavidBowie in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 23rd, 2011, 02:03 PM
  2. Help with creating a dice rolling program in Java
    By lilmiss in forum Object Oriented Programming
    Replies: 4
    Last Post: October 26th, 2011, 09:27 PM
  3. Command For Rolling Random Number
    By Brickwood in forum Java Theory & Questions
    Replies: 7
    Last Post: March 26th, 2011, 03:18 AM
  4. [SOLVED] Dice Rolling Simulation
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2011, 06:51 PM
  5. Die Rolling - Continuing the statement?
    By Override in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 29th, 2010, 01:21 AM