![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 2
Rep Power: 0
![]() |
Ok being as I am new to this forum, and to java itself, I am seeking help form someone hwo is willing to solve my simple(to you) but complicated(for me) task of completeing my RollDie array program. I so farr have this array, which works and everything, ie. displays the output and the number of times each face of the die came up, but my modification which I cannot seem to get to work is a trap for the very first roll, the program should display the first roll(side 1 or 2or 3 etc...), and then the number of times each side came up etc...after which it will check the first rolled side against all the rolled sides, where upon if the first rolled side is not equal to the side with the most hits, a statment will output declaring false, otherwise if it does match up the output would be true. Much thanks are indebted to anyone who can help me with this program.
[code] import javax.swing.*; public class RollDie { public static void main(String[] args) { int numFlips = 100; int randNum; int frequency[] = new int[7]; for (int flip = 1; flip <= numFlips; flip++) { randNum = 1 + (int)(Math.random() * 6); frequency[randNum]++; } String output = "Face:\tFrequency:\n"; for(int face = 1; face < frequency.length; face ++) { output += "\n" + face + "\t" + frequency[face]; } JTextArea outputArea = new JTextArea(); outputArea.setText(output); JOptionPane.showMessageDialog(null, outputArea, "Rolling a Die", JOptionPane.WARNING_MESSAGE); System.exit(0); } } [\CODE] |
|
|
|
|
|
#2 |
|
Programmer
|
Instead of checking agianst each one each time, I'd add another variable that keeps track of the most frequent die roll. Also, every time the frequency of a face increases, check that new frequency against the highest to see if it's taken the lead. As for the rest, what do you mean "a statement will output?" To the console? To your GUI? And what do you mean by "the first roll?" After just the first roll, there won't be any data yet to compare it too or display. What's your overall objective, to prevent a number from being roled if it's already the most frequent?
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
Also in the true nature of OO approach instead of having class roll die, make the die an object with a roll function in it. Thenyou only have to call the function each time (saves on code in some senes )
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2005
Posts: 2
Rep Power: 0
![]() |
Ed your reply has helped me so much, thanks
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|