# Point in the right direction? Dice

• 02-11-2011, 10:33 PM
misterwebb
Point in the right direction? Dice
Okay so i have a program i need to make for my class. Dont want anyone to really solve it just need a push in the right direction and hopefully i can do the rest sooo here it goes.

The program simulates rolling a die with 6 sides. It should ask the user if they would like to continue accumulating the frequency of rolls stored in a data file, if the file exists. If no files exists, then the program creates one and stores the results at the end of the program. Then the program should ask the user the number of times to roll the die. The program should then simulate the rolling of the die and output the data to a dialog box as a table containing column headers of face and frequency. Frequency is the number of times each face was rolled. The program should also save the frequency of roles back to the file as accumulated data. Do not delete the data in the file. Instead the frequency of each face should be an accumulation of rolls from each run of the program. According to the rules of probability, the more times you roll the die, the closer the frequencies should get to being equal. Use error checking to ensure the user types in valid information.

Any help with this would be much appreciated, new to programming and just reading this makes me want to drop the class haha.
• 02-11-2011, 10:40 PM
Fubarable
I think the first place to start would be this article: So, You Need to Write a Program but Don't Know How to Start. I know that it has helped me and may help you.

Luck!
• 02-11-2011, 10:51 PM
misterwebb
lol from the title it sounds up my alley for sure
• 02-13-2011, 06:32 PM
misterwebb
Okay so now im to the point i know i need to import a java.util.random. and use one integer variable such as num1 = randomNumbers.nextInt(6); But how would i make it execute the number of times the user says to roll the die? Still very confused on this one all the way through
• 02-13-2011, 06:36 PM
Fubarable
Quote:

Originally Posted by misterwebb
Okay so now im to the point i know i need to import a java.util.random. and use one integer variable such as num1 = randomNumbers.nextInt(6); But how would i make it execute the number of times the user says to roll the die? Still very confused on this one all the way through

You would get the user input as an int, and then use this int at the top bound of a for loop. In pseudo-code:

Code:

```get input from user and place in int variable rollDiceTimes for loop from int i = 0 to i < rollDiceTimes   roll my dice and do something with the result. end for loop```
• 02-13-2011, 06:56 PM
misterwebb
So something like this?

Code:

```system.out.print("How many times do you want to roll the die"); rollTimes = keyboard.nextInt(); for(int i = 0, i < rollTimes) {   Confused in this part, the actual rolling of the die?     //Write the results to a file   outputFile.println(dieRoll); }```
• 02-13-2011, 10:05 PM
Yes - that is better. Do you see the last part of your code that says:

outputFile.println(dieRoll); ** dieRoll**

the middle of your loop, tell the computer what dieRoll is. Know what I mean?

Your for loop is going through the whole rollTimes - which is good. While the computer is doing that make the number of times you had to go through the whole program to DieRoll.

I am not an experienced programmer by any means, but I have taken a couple of java classes.
• 02-13-2011, 11:12 PM
misterwebb
Quote:

Yes - that is better. Do you see the last part of your code that says:

outputFile.println(dieRoll); ** dieRoll**

the middle of your loop, tell the computer what dieRoll is. Know what I mean?

Your for loop is going through the whole rollTimes - which is good. While the computer is doing that make the number of times you had to go through the whole program to DieRoll.

I am not an experienced programmer by any means, but I have taken a couple of java classes.

so inside my for loop i should put
Code:

``` dieRoll = randomNumbers.nextInt(6); BUT how would i display the results correctly? it needs to have the number 1 through 6 in one column then the frequency in the column beside it.```
• 02-13-2011, 11:23 PM
Fubarable
Don't worry about displaying things from within the for loop as this is only for getting roll results. You'll need an array, and then depending on the roll result in the for loop, increase the corresponding array element by one. So, if the die rolls 3, then increase myArray[3] by one and store it back into the myArray[3] spot. You would also use the same array to store numbers from the file and to write numbers to a file and for display, but all of this later stuff is done after the for loop has concluded.