# Slick Java

#### Homework help - Dice Roll

###### Rate this Entry
by , 04-28-2013 at 02:03 AM (2668 Views)
Originally Posted by Slick Java
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]);
}

}

}```

Tags: None
Categories
Uncategorized