Results 1 to 3 of 3
  1. #1
    Slick Java is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Blog Entries
    1
    Rep Power
    0

    Default Homework help - Dice Roll

    Hi, I am having trouble with this program. The point of this program is to roll a dice, store dice values in an array and print out how much time the value has been occurred. The output should be like this:

    1 3

    4 1

    5 1

    2 1

    But I am getting this:

    1 3

    4 1

    5 1

    1 3

    2 1

    1 3


    I am trying to find a way to get my expected result. The code is below.



    Java Code:
    package com.gavin.work;
    
    import java.util.Random;
    
    public class DiceRoll
    {
    
        private final Random ran;
    
        int rolls;
    
        int numberOfDice = 1;
    
        int numberOfThrows = 6;
    
        int numberOfSides = 6;
    
        int[] counts = new int[numberOfThrows];
    
        int[] occurences = new int[numberOfSides * numberOfDice];
    
        public DiceRoll()
        {
            ran = new Random();
    
        }
    
        public static void main(String args[])
        {
            DiceRoll d = new DiceRoll();
            d.go(Integer.parseInt(args[0]),
                Integer.parseInt(args[1]),
                Integer.parseInt(args[2]));
    
        }
    
        public int rollDice(int i)
        {
            return ran.nextInt(numberOfSides) + 1 * i;
        }
    
        public void go(int i, int j, int p)
        {
            numberOfDice = i;
            numberOfThrows = j;
            numberOfSides = p;
            doWork();
    
        }
    
        public void printAmounts()
        {
            System.out.printf("%d \n %d \n %d", numberOfDice, numberOfThrows,
                numberOfSides);
        }
    
    
        public void doWork()
        {
            for (int h = 0; h < occurences.length; h++)
            {
                occurences[h] = 0;
            }
    
            for (int i = 0; i < counts.length; i++)
            {
                counts[i] = rollDice(numberOfDice);
            }
    
            for (int i : counts)
            {
                for (int j = 0; j < occurences.length; j++)
                {
                    if (counts[j] == i)
                    {
                        occurences[j] = occurences[j] + 1;
                    }
                }
            }
    
            for (int k = 0; k < counts.length; k++)
            {
                System.out.printf("%d  %d\n\n", counts[k], occurences[k]);
            }
    
        }
    
    }
    Last edited by Slick Java; 04-28-2013 at 12:19 AM.
    My website - Java Organisation
    Jason Xu

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Homework help - Dice Roll

    The sizes of your arrays and the way you're iterating over them don't seem to make sense. What are counts and occurences supposed to represent? Why are you initializing them with potentially different sizes (lines 18-20) but then iterating over them with a single counter as if they were parallel arrays (lines 81-84)?
    Get in the habit of using standard Java naming conventions!

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: Homework help - Dice Roll

    What are the possible outcomes of a die? 1,2,3,4,5 and 6 (or more, but you could make that a variable). You could use those values (minus one) as indices for the array of outcomes. This would be massively simpler:

    This would be your entire loop in a sort of pseudocode:
    Java Code:
    int outcomes[] = new int[6]; //there are 6 sides
    for ( int i = 0; i < numberOfThrows; i++ ) {
       int random = ran.nextInt(6) + 1; // this is the actual number from 1 to 6
       outcomes[random - 1]++; //This needs -1 because array indices start at 0
    }
    Don't overcomplicate things :)

Similar Threads

  1. Problem in Finding the Mode of Dice Roll Outcomes? Help?
    By Kratos321 in forum Advanced Java
    Replies: 1
    Last Post: 05-11-2011, 03:21 AM
  2. Need help with AI in roll dice game
    By shazakala in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 11:49 AM
  3. HELP WITH DICE ROLL PROBLEM (netbeans)
    By IdrinkJava in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 03:55 PM
  4. Roll The dice
    By Subhanrukh in forum New To Java
    Replies: 8
    Last Post: 04-19-2010, 08:39 AM
  5. Roll dice class with three dices
    By nube07 in forum New To Java
    Replies: 4
    Last Post: 07-14-2008, 01:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •