Hello Everybody. This program needs to use an array to count the amount of times a certain roll appears on the dice, after the user enters how many dice and rolls they would like to use. I was able to figure out the code to print out the values of each roll, however I'm not sure what I would have to do to use an array as a counter. Any help would be appreciated.

Example Results:

You Rolled 2: 1 Time
You Rolled 3: 0 Times
You Rolled 4: 3 Times

Here is my code so far:

import java.util.*;

public class Dice {

	public static Scanner in = new Scanner (System.in);

	public static void main (String[] args) {

	    int dice = 0;
		int roll = 0;

		while (true) {

		   	System.out.print ("How Many Dice Do You Want To Roll?  ");
		   		dice = in.nextInt();
		   			if (dice > 0) break;
		   	System.out.println ("Must Be Positive!");


		while (true) {
		   	System.out.print ("How Many Times Do You Want To Roll?  ");
		   		roll = in.nextInt();
		   			if (roll > 0) break;
		   	System.out.println ("Must Be Positive!");


		int dicetotal = Dicecount (dice);

			for (int i = 0; i < roll; i++) {
			 System.out.println (Dicecount(dice));


	public static int Dicecount (int dice) {

		int dicetotal = 0;

		  for (int x = 0; x < dice; x++) {

			int rollcount =(int)(1+6*(Math.random()));
			dicetotal+=rollcount; }

			return dicetotal;



Recommended Answers

All 4 Replies

I haven't read the code, but perhaps something like this...
You have an array of length 6 eg dice[].
Each time you get a 1, you do dice[0]++;
Each time you get a 2, you do dice[1]++;

In general, you do dice[num-1]++;

saves you having 6 variables for each different dice outcome.

Any help?

The only question I have is how I would go about this with values that are unknown before the user puts them in. The rolls of the dice could be from 1 - possible millions.

I don't get it, are you using normal dice with 1-6 on?

I got the program to work with the user entering the values he/she wants. Thanks for all your help. This would have taken me ages to figure out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.