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

Thread: Java Dice Program

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Dice Program

    public class RollDice2
    {
     public static void main(String [] args)
     {
     Scanner input= new Scanner(System.in);
     
     int dice=0,rolls=0;
     
     System.out.println("Enter the number of dice you wish to use");
     dice=input.nextInt();
     
     System.out.println("Enter the number of rolls you wish to use");
     rolls=input.nextInt();
     int frequencies[ ] = new int[6*dice + 1];
     // set each cell in frequencies to 0
     for (int i=0; i<frequencies.length; i++)
       frequencies[i] = 0;
     // roll the dice
     for (int i=0; i<rolls; i++) {
       int total = RollDice2.throwDice(dice);
          //System.out.print("-");
     
       frequencies[total]++;
      for(int s=0;s<total; s++)
          System.out.println(total+"- ");
     
     }
     
    }
    public static int throwDice(int count){
     
    		 count =(int)(1+6*(Math.random()));
     
     
     
     
     
    return count;	}
    }

    I am trying to write a program and the user inputs how many dice and rolls they want. The output is a histogram using dashes. I'm not sure what is wrong with my code at this point.
    Last edited by helloworld922; May 22nd, 2011 at 08:02 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java Dice Program

    Please properly format your post. Use highlight tags around your code. And please don't type in all caps. That usually has the connotation of shouting.

    What is wrong? Are you getting an exception? If so, please post the error message. Otherwise, please post an example input and the output you're getting, along with a statement of what the correct output should be.

  3. #3

    Default Re: Java Dice Program

    It works when I run and compile it except the - are after the number. Also you should have an import statement at the top declaring your importing the scanner class.
    For great tutiorals on java programming, visit my blog: http://www.ridgehutchingsblog.blogspot.com/

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Dice Program

    2 -
    3 --
    4 ---
    5 ----
    6 -----
    7 ------
    8 -----
    9 ----
    10 --
    11 --
    12 -
    the above is the correct output
    this is the output i am getting is


    Enter the number of dice you wish to use
    2
    Enter the number of rolls you wish to use
    2
    2-
    2-
    5-
    5-
    5-
    5-
    5-
    Press any key to continue . . .




    import java.util.*;
     
    public class RollDice2
    {
     public static void main(String [] args)
     {
     Scanner input= new Scanner(System.in);
     
     int dice=0,rolls=0;
     
     System.out.println("Enter the number of dice you wish to use");
     dice=input.nextInt();
     
     System.out.println("Enter the number of rolls you wish to use");
     rolls=input.nextInt();
     int frequencies[ ] = new int[6*dice + 1];
     // set each cell in frequencies to 0
     for (int i=0; i<frequencies.length; i++)
       frequencies[i] = 0;
     // roll the dice
     for (int i=0; i<rolls; i++) {
       int total = RollDice2.throwDice(dice);
          //System.out.print("-");
     
       frequencies[total]++;
      for(int s=0;s<total; s++)
          System.out.println(total+"- ");
     
     }
     
    }
    public static int throwDice(int count){
     
    		 count =(int)(1+6*(Math.random()));
     
     
     
     
     
    return count;	}
    }
    Last edited by ebone; May 22nd, 2011 at 08:41 PM.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Dice Program

    yes it works fine when i do it too but the results are not what they should be. i have the import statment at the stop of my code i just forgot to copy and past it

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Java Dice Program

    There are so many things wrong with your code. I'll start with the throwDice method.

    Why bother passing in a value as a parameter if you immediately throw it away and assign a new value to the count variable?

    Why are you not using the parameter in your formula to calculate the dice total?

    The formula will not give correct results. The Math.random class generates numbers from 0.0 to .999999. Plugging those values into your formula:

    1 + (6 * 0.0) = 1
    1 + ( 6 * 0.999999) = 6

    Therefore your method can only return values in the range of 1 ( which is impossible for 2 or more dice) to 6. What happens when there are two or more dice? Where are the values of 7 and upwards?

    Now back to the main method:

    Why do you have the loop displaying the histogram inside the other loop? I would expect the program to display it once at the end.

    Why is the histogram display loop iterating "total" number of times? If the total is 2 how is it supposed to display the histogram for all other values above 2?

Similar Threads

  1. [SOLVED] Dice Rolling Simulation
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2011, 05:51 PM
  2. Dice Game help and Arrays
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 9th, 2011, 09:08 PM
  3. [SOLVED] Help printing random dice
    By vanDarg in forum Java Theory & Questions
    Replies: 12
    Last Post: February 1st, 2011, 02:09 PM
  4. Dice Counter
    By Xxl0n3w01fxX in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2011, 10:49 AM
  5. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM

Tags for this Thread