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.


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

Results 1 to 3 of 3

Thread: HELP random generator with roll() and print() method

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    Default HELP random generator with roll() and print() method

    so i'm trying to make a program where once the die is rolled, then a picture prints...
    my random generator is working properly, but its my print() method thats not.. once i've rolled the die, it gives a random number, but once i do the print method, the topValue turns into a negative which ruins my print() method.. HELP please, i'm not sure what im doing wrong??????

    once i run my main method which is this...

    public class DieTest
    public static void main(String[] args)
    Die die1=new Die();
    die1.print(); //topValue turns into negative =( .. so it doesnt follow what it was in the roll() method


    import java.util.Random;
    public class Die
    private int topValue;
    private int getTopValue()
    return topValue;
    public void roll()
    Random rndm1= new Random();
    for(topValue = 1; topValue >= 1; topValue++);
    int topValue = rndm1.nextInt(6);
    public void print()
    if(topValue == 1)
    System.out.print( "+-------+\n| |\n| o |\n| |\n+-------+\n" );
    else if(topValue == 2)
    System.out.print( "+-------+\n| o |\n| |\n| o |\n+-------+\n" );
    else if(topValue == 3)
    System.out.print( "+-------+\n| o |\n| o |\n| o |\n+-------+\n" );
    else if(topValue == 4)
    System.out.print( "+-------+\n| o o |\n| |\n| o o |\n+-------+\n" );
    else if(topValue == 5)
    System.out.print( "+-------+\n| o o |\n| o |\n| o o |\n+-------+\n" );
    System.out.print(topValue +"+-------+\n| o o |\n| o o |\n| o o |\n+-------+\n" );

  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    North Wales
    My Mood
    Thanked 115 Times in 106 Posts
    Blog Entries

    Default Re: HELP random generator with roll() and print() method

    Next time, highlight your code using the highlight tags found in my signature.
    for(topValue = 1; topValue >= 1; topValue++);
    Notice the semi-colon at the end. What this will now do is increment for as long as topValue is equal to or greater than 1.
    Integers may only extend to a certain value - a cap if you like, where if it passes its cap it turns negative, which is what is happening to you.

    The reason you even attempted to use a for-loop baffles me, as int topValue = rndm1.nextInt(6); is sufficient.
    Also topValue has already been defined, so no need to call int before it.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Join Date
    Aug 2011
    My Mood
    Thanked 4 Times in 4 Posts

    Default Re: HELP random generator with roll() and print() method

    I'm having the same issue. I have found a work around.


    Hope that helps. You do have other flaws in that code however. First you use topValue in a loop, then you set topValue to a random which breaks your loop. these should be 2 different variables.
    Last edited by Spidey1980; August 18th, 2011 at 01:54 PM.

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Method to print a diamond
    By snowMac in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 8th, 2011, 04:18 AM
  3. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 12:00 PM
  4. Replies: 0
    Last Post: January 25th, 2011, 01:24 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM

Tags for this Thread