Results 1 to 14 of 14
 12012011, 06:13 AM #1Member
 Join Date
 Dec 2011
 Posts
 4
 Rep Power
 0
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 }
 12012011, 06:20 AM #2
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.
 12012011, 03:54 PM #3Member
 Join Date
 Dec 2011
 Posts
 4
 Rep Power
 0
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.
 12012011, 07:56 PM #4Senior Member
 Join Date
 Dec 2011
 Posts
 102
 Rep Power
 0
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:
http://www.javaforums.org/newjava/...m2cubes.html
hope it helps!
 12012011, 09:06 PM #5
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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.
 12012011, 09:08 PM #6
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.
 12012011, 10:55 PM #7
Re: Dice help. posting the number of times a number is rolled.
 12022011, 05:34 AM #8Member
 Join Date
 Dec 2011
 Posts
 4
 Rep Power
 0
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 tenthousand 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")); } } }
 12022011, 06:12 AM #9
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.
 12022011, 06:16 AM #10
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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.
 12022011, 06:23 AM #11
 12032011, 01:47 AM #12Member
 Join Date
 Dec 2011
 Posts
 4
 Rep Power
 0
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
 12032011, 03:38 AM #13
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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];
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(...)
 12052011, 12:08 AM #14
Re: Dice help. posting the number of times a number is rolled.
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

smallest number and largest number using while and if statements
By vicu1 in forum New To JavaReplies: 26Last Post: 11142011, 03:22 PM 
Code to convert binary number to base 10 number
By j@y in forum New To JavaReplies: 1Last Post: 10282011, 08:18 AM 
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02152010, 01:37 AM 
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02052010, 11:42 PM 
Problem calling classes to flip coin x number of times and record heads or tails
By adlb1300 in forum New To JavaReplies: 2Last Post: 11112007, 09:07 AM
Bookmarks