Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Junky

Thread: Dice help. posting the number of times a number is rolled.

  1. #1
    cookiejarvus is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Dice help. posting the number of times a number is rolled.

    What I need is a program to roll a die 10000 times and count the number of 1s rolled, 2s rolled, etc.. After that I need the program to use JOptionPane to post the total tally for each of the rolls in one window.
    so far this is what I have:
    Java Code:
    import javax.swing.*;
    02
    public class Dice
    03
     
    04
    {
    05
        public static void main(String[] args)
    06
            {
    07
     
    08
     
    09
                int[] A = new int[10000];
    10
     
    11
                for(int i=0; i<A.length; i++)
    12
                    A[i] = 1 + (int) (6*Math.random());
    13
                     
    14
                    for(int i=0; i<A.length; i++)
    15
                        System.out.println(A[i]);
    16
     
    17
                    }
    18
    }
    What I need now is to somehow tally the numbers and post them in the JOptionPane window. I'm pretty sure I need to create a string but I don't know where to start.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Dice help. posting the number of times a number is rolled.

    Why do you have a 10000 long array? Do you plan on keeping every number rolled? If so why? I think a better idea would be to have an array to store the counts of each number rolled, if it is 1 increment the 1 count, if it was 2 increment the 2 count etc.
    Herah likes this.

  3. #3
    cookiejarvus is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Dice help. posting the number of times a number is rolled.

    Why do you have a 10000 long array? Do you plan on keeping every number rolled? If so why? I think a better idea would be to have an array to store the counts of each number rolled, if it is 1 increment the 1 count, if it was 2 increment the 2 count etc.
    Well the assignment calls for a total of 10,000 rolls, but if there is an easier way to do this while still using Math.random and the 10,000 rolls i'm all ears.

  4. #4
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Dice help. posting the number of times a number is rolled.

    I've got similar program to make... i'll link u to a post about it:

    probability- sum of 2 cubes


    hope it helps!

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Dice help. posting the number of times a number is rolled.

    Make an array of size x, where x is the number of sides on the die. From there you increment the correct slot in the array each time you roll. Junky hinted at this as well. Give it a shot and see how it works out.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Dice help. posting the number of times a number is rolled.

    Your program only requires how many times something has been rolled. IE for a dice your options are 1,2,3,4,5,6. That means your array only needs to be 6 slots.

    Run your Math.Random, then do a logical statement to detect what digit has been rolled. Assign each array slot a integer variable initialized to zero and assign it to a number corresponding to a side on the die. Then increment the variable in the array slot every time the side of the die is rolled.

    Simple really, just got to put your noggin into it. You may also be able to use a primitive in the array too, depending on what you plan on doing after the dice are rolled. If all you are doing is rolling, storing and then print after 10,000 rolls then use a primitive.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Dice help. posting the number of times a number is rolled.

    Quote Originally Posted by cookiejarvus View Post
    Well the assignment calls for a total of 10,000 rolls, .
    Where did I say "Don't roll 10,000 times"? What I did say was don't store those 10,000 rolls. Only store the current roll and 6 counts (one for each number). Just like you would on paper if I were to call out the 10,000 rolls to you.

  8. #8
    cookiejarvus is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Dice help. posting the number of times a number is rolled.

    Ok so while I was in class the other day I asked some peers if they had any input. They told me that I need to include a do/while method. I took that input and tried to build off of it. So far I've managed to put together this jumble of nonfunctional code.

    Java Code:
    import javax.swing.*;
    public class DiceW
    {
    
        public static void main(String[] args)
    
            {
    
                       int num1, num2, num3, num4, num5, num6;
    
       int num1 = 0;
            int num2 = 0;
            int num3 = 0;
            int num4 = 0;
            int num5 = 0;
            int num6 = 0;
    
    
        int total = 10000;
    
    
    
    
                String choice="";
                do
                {
    
    
                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++)
                    {
                       dice = (int)(Math.random90*6+1);
                       sum = (dice);
                       switch (sum)
                       {
                           case 1 : num1++;
                           case 2 : num2++;
                           case 3 : num3++;
                           case 4 : num4++;
                           case 5 : num5++;
                           case 6 : num6++;
                            break;
                       }
    
                        System.out.println(A[i]);
    
                   JOptionPane.showMessageDialog(null, "The dice has been rolled ten-thousand times, here is the statistics" +
                                                     "\nThe number of 1s" + "=" + num1 +
                                                     "\nThe number of 2s" + "=" + num2 +
                                                     "\nThe number of 3s" + "=" + num3 +
                                                     "\nThe number of 4s" + "=" + num4 +
                                                     "\nThe number of 5s" + "=" + num5 +
                                                     "\nThe number of 6s" + "=" + num6);
    
                   choice = JOptionPane.showInputDialog("Continue?");
                        } while(choice.equals("yes") | choice.equals("yes"));
    
    
                }
          }
    
    }
    The Choice to continue at the end is one of the requirements of the assignment so i'm pretty sure that I have to use the do/while method and build off that. Hope that makes sense. I only get 4 errors when compiling so I think im on the right track.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Dice help. posting the number of times a number is rolled.

    I still do not understand why you want a 10,000 long array to store all the numbers. You need to revise switch statements. If you are getting error messages then you should post the exact messages here since we do not read minds.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Dice help. posting the number of times a number is rolled.

    You honestly don't even need switch statements. Here is some pesudocode:

    Java Code:
    declare array of length n //n is the number of sides a die has
    loop
      increment a random element in the array
    end loop

    The whole thing should be possible in ~ 10 lines of code.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Dice help. posting the number of times a number is rolled.

    Quote Originally Posted by sunde887 View Post
    You honestly don't even need switch statements. .
    Yep that was my advice in the first reply but it seems all advice is being ignored.

  12. #12
    cookiejarvus is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Dice help. posting the number of times a number is rolled.

    Its not that your advice is being ignored its that I do not understand java therefore, I do not know how to follow your advice. You need to be really specific with me and include examples otherwise, I have no clue what your talking about.

    as far as the errors I receive they are as follows:

    I:\Java\Project 2\DiceW.java:63: error: while expected
    }
    ^
    I:\Java\Project 2\DiceW.java:66: error: illegal start of expression
    }
    ^
    I:\Java\Project 2\DiceW.java:66: error: reached end of file while parsing
    }
    ^
    I:\Java\Project 2\DiceW.java:67: error: reached end of file while parsing
    4 errors

    Tool completed with exit code 1

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Dice help. posting the number of times a number is rolled.

    When you declare an array you specify the length as well
    Java Code:
    int[] x = new int[50];
    In this case, the array x would have a length of 50.

    Since there are only 6 sides on the die you will only need an array which is 6 ints long. After creating this, start a loop which loops 10,000 times and increment the correct spot based on a random number. When indexing into an array you are able to index with the return value of Random.nextInt(...)

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Dice help. posting the number of times a number is rolled.

    Quote Originally Posted by cookiejarvus View Post
    Its not that your advice is being ignored its that I do not understand java
    In my first post I advised that you cahnge the 10,000 long array to one that was only 6 long. Yet in your code you have not done that. Since you have managed to declare an array then the only conclusion I can arrive at is that you understand enough Java to do this and therefore my advice is being ignored. If at any time you did not understand the point I was trying to make then ask don't ignore and continue stumbling along without a clue.

Similar Threads

  1. Replies: 26
    Last Post: 11-14-2011, 03:22 PM
  2. Replies: 1
    Last Post: 10-28-2011, 09:18 AM
  3. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  4. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 11:42 PM
  5. Replies: 2
    Last Post: 11-11-2007, 09:07 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
  •