# Thread: Point in the right direction? Dice

1. Member
Join Date
Feb 2011
Posts
7
Rep Power
0

## 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.

2. Member
Join Date
Feb 2011
Posts
7
Rep Power
0
lol from the title it sounds up my alley for sure

3. Member
Join Date
Feb 2011
Posts
7
Rep Power
0
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

4. 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:

Java 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```

5. Member
Join Date
Feb 2011
Posts
7
Rep Power
0
So something like this?

Java 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);
}```

6. Member
Join Date
Aug 2010
Posts
70
Rep Power
0
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.

7. Member
Join Date
Feb 2011
Posts
7
Rep Power
0
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
Java 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.```

8. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•