I'm working on a program for class that uses the method Math.random( ) to generate random integers between 1 and 6. Then generate ten-thousand numbers between 1 and 6. Then calculate the number of 1 that has appeared, the number of 2 that has appeared, and so on. The final output must be on a dialog box (using JOptionPane). The process must be looped until the user decides to quit. The output format should like the following:
*****************************************
The dice has been rolled ten-thousand times, here is the statistics
The number of 1s appeared is 2000
The number of 2s appeared is 2000
The number of 3s appeared is 2000
The number of 4s appeared is 2000
The number of 5s appeared is 1000
The number of 6s appeared is 1000
Do you want to try again (Y/N)?:
*********************************************

This is what I have so far. I can get it to roll the die 10,000 times, but i cant figure out how to get the program to count the total for each number rolled and post it using JOptionPane.

import javax.swing.*;
public class Dice

{
	public static void main(String[] args)
		{
			String choice="";
			do
			{
				int total1=0, total2=0, total3=0, total4=0, total5=0, total6=0;

			int[] A = new int[10000];

			for(int i=0; i<A.length; i++)
				A[i] = 1 + (int) (6*Math.random());

				total1==
				total2==
				total3==
				total4==
				total5==
				total6==



				for(int i=0; i<A.length; i++)
					System.out.println(A[i]);

				}
			{
			JOptionPane.showMessageDialog(null,"The number of 1s appeared is" + total1, "The number of 2s appeared is" + total2, "The number of 3s appeared is" + total3, "The number of 4s appeared is" + total4, "The number of 5s appeared is" +total5, "The number of 6s appeared is" +total6);
			choice = jOptionPane.showInputDialog("Recalculate? (Y=yes|N=no)");
		}while (choice.equlas("Y"));

}

Don't do it like that. It takes too much time and wastes to much memory. Instead of creating an array with every number, try switching the random number generated:

switch(generatedNumber){
  case 1:
      total1++;//increment the total of number 1
  case 2:
  ...
}

There are other ways to get the same result like randomizing the total count of all numbers (1 to 6). It would be much faster.

Instead of declaring separate counts (named total1, total2, etc.),
Make this an array (of length [6]).
Now, rework your logic.
You already know what the number being randomly generated (A).

Don't do it like that. It takes too much time and wastes to much memory. Instead of creating an array with every number, try switching the random number generated:
Java Syntax (Toggle Plain Text)
switch(generatedNumber){
case 1:
total1++;//increment the total of number 1
case 2:
...
}
There are other ways to get the same result like randomizing the total count of all numbers (1 to 6). It would be much faster.

OK so this is what I ended up doing (almost certain its wrong)

import javax.swing.*;
public class Dice2

{
    public static void main(String[] args)
        {

            int no1=0, no2=0, no3=0, no4=0, no5=0, no6=0;

            int[] A = new int[10000];

            for(int i=0; i<A.length; i++)
                A[i] = 1 + (int) (6*Math.random());

                for(int i=0; i<A.length; i++)
                    System.out.println(A[i]);

                switch(10000){
                  case 1:
                      no1++;//increment the total of number 1
                  case 2:
                      no1++;
                  case 3:
                      no3++;
                  case 4:
                      no4++;
                  case 5:
                      no5++;
                  case 6:
                      no6++;

                }

         }
}

It still generates 10000 random numbers b/t 1 and 6. However, I need to figure out how to tally how many times each number(1-6) appears and have it post in a JOptionPane. Might I add that I don't understand Java very well and if you could go into more detail that would be excellent.

you have to switch the number 'A' in your case. Don't forget adding the break statement before each case (I forgot it). It would look like this:

switch(A[i]){
   case 1:
      no1++;
      break;
   case 2:
      ...
}

If you see you don't need variable 'A' as an array in this part. You can change it by a type 'int'. If you don't understand switchs go to this page.

Edited 5 Years Ago by teo236: n/a

OK I have made some good progress on this program. I finally figured out how to get the program to count the number of times each number is rolled.

This is what I have so far.

import javax.swing.*;
 import java.util.*;
 public class DiceW

{
        public static void main(String[] args)
    {


    int[] A = new int[10000];

                for(int i=0; i<A.length; i++)
                    A[i] = 1 + (int) (6*Math.random());

                    for(int i=0; i<A.length; i++)
                    System.out.println(A[i]);

    int[] total = new int[6];

            Random random = new Random();

            for(int i = 0; i < 10000; i++)

            total[random.nextInt(6)]++;


            for(int i = 0; i < 6; i++) {

                JOptionPane.showMessageDialog(null,"Total number of " + (i+1) + " = " + total[i]);



        }
    }
}

The only thing I need now is to get all six totals to show up in one JOptionPane window instead of the individual windows.

Try creating a string that will be the result of a set of strings using your loop. Something like this:

String result="";
for(int i = 0; i < 6; i++) {
   result+="Total number of " + (i+1) + " = " + total[i];
}

and then show your dialog.

OK i made some excellent progress, but I still have one setback. I have been trying to figure out where I put the do/while statement so that the program will ask me if I want to continue or not. Other than this hiccup the program does everything it needs to do as far as counting the number of occurrences etc. If you guys could take a look at my code and point me in the right direction that would be great. Thanks.

import javax.swing.JOptionPane;

public class DiceF
    {
  public static void main(String[] args)

    {
          do
          {

        //create array of 10,000 randomly geberated numbers
        int[] randomNumbers = new int[10000];
        for(int i = 0; i < randomNumbers.length; i++)
            randomNumbers[i] =1 +(int)(Math.random() * 6);


        for (int i = 0; i < randomNumbers.length; i++)
            {
           if ((i + 1) % 1 == 0)
            System.out.println(randomNumbers[i] + " ");
            else
            System.out.print(randomNumbers[i] + " ");
            }

            {
        int[] counts = countInts(randomNumbers);

            displayIntCount(counts);
            }
            }

        public static int[] countInts(int[] ints)
        {
        int[] counts = new int[6];

        for(int i = 1; i <=counts.length; i++)
        for(int j=0;j<ints.length;j++)
        if(ints[j] == i)
        counts[i-1]++;

            return counts;
        }

        public static void displayIntCount(int[] counts)
        {
        //displays the number of occurrences
        for (int i = 0; i < counts.length; i++)
                JOptionPane.showMessageDialog(null, "The number 1 occurs "+counts[i]+" times \nThe number 2 occurs "+counts[i+1]+" times \nThe number 3 occurs "+counts[i + 2]+ " times \nThe number 4 occurs " +counts[i+3]+ " times \nThe number 5 occurs " +counts[i + 4]+ " times \nThe number 6 occurs "+counts[i + 5]+ " times");



                    String s = JOptionPane.showInputDialog("Do it again (1 yes, 2 no)");
                    choice = Integer.parseInt(s);
                    } while(choice==1);

        }

}
for (int i = 0; i < counts.length; i++)
                JOptionPane.showMessageDialog(null, "The number 1 occurs "+counts[i]+" times \nThe number 2 occurs "+counts[i+1]+" times \nThe number 3 occurs "+counts[i + 2]+ " times \nThe number 4 occurs " +counts[i+3]+ " times \nThe number 5 occurs " +counts[i + 4]+ " times \nThe number 6 occurs "+counts[i + 5]+ " times");

This will definately go wrong.
You show this message 5 times, which you don't need.
When you run this code, you will get an IndexOutOfBoundException, because you want to get counts[5+5], which does not exist.
Just call that line once, without the for loop, and it'll work.

Now for the do {} while {}:
- Make a method that runs the test with the dice.
- In the main method, put the following:
- a do, while loop
- the call to the method
- a prompt for the user to check if the user wants to run the program again

If you run into problems, don't hesitate to ask!

The do while loop you must put it in the whole main in your case:

public static void displayIntCount(int[] counts)
{
   do{
      ...//this doesn't include other methods like 'displayIntCount' or 'countInts'
   }while(choice==1);
}

hiddepolen is right, it won't work like you are trying. You only have to show your dialog once. First create a String containing the message that the dialog must show and then show your dialog with the string message:

String message="";
...
JOptionPane.showMessageDialog(null, message);
This article has been dead for over six months. Start a new discussion instead.