943,548 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2975
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 20th, 2008
0

Help with Dice Simulation

Expand Post »
Okay so the program I was asked to design requires the simulation of rolling three dice and printing out the outcome of each turnout while adding to a counter until all three dice are different numbers. So a sample output would be:

5 6 2
count: 1
or
3 4 3
5 2 2
4 6 4
1 3 2
count: 4

So far I have the following code:

import java.util.Random;
import apcslib.*;

public class Rolling
{
public static void main(String[] args)
{
private Random myDie;

double seed = Math.random() * 10;
int seedInt = (int)seed;
myDie = new Random (seedInt);
int counter = 0;

do
{
int dieRollOne = die.nextInt(6) + 1;
int dieRollTwo = die.nextInt(6) + 1;
int dieRollThree = die.nextInt(6) + 1;
counter += 1;
System.out.println(dieRollOne + " " + dieRollTwo + " " + dieRollThree);
}
while ((dieRollOne == dieRollTwo) || (dieRollOne == dieRollThree)
|| (dieRollTwo == dieRollThree));

if(!(dieRollOne == dieRollTwo) && !(dieRollOne == dieRollThree)
&& !(dieRollTwo == dieRollThree));
{
System.out.println("Count = " + counter);
}


}
}


I've received an illegal start of expression for:
private Random myDie;
and for the initialization of do on:
do
{
. . .

If anyone can give any assistance I would be greatly appreciative.
Similar Threads
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Nov 20th, 2008
0

Re: Help with Dice Simulation

You can' use access modifiers inside of a method and you can't use variables declared inside of a loop as part of that loops conditional expression.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 20th, 2008
0

Re: Help with Dice Simulation

What do you mean by access modifiers? I get what you said about the variable declaration inside the loop. I also still get an error for private Random myDie; and I don't know why.
Last edited by IMtheBESTatJAVA; Nov 20th, 2008 at 9:03 am.
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Nov 20th, 2008
0

Re: Help with Dice Simulation

private, public, protected are access modifiers. They only apply to class and instance variables, not local variables declared inside of a method (which myDie is).

Also, in a loop, if you want to use a variable (lets call it var) in the conditional (i.e. in the var != whatever part), that variable cannot be declared inside of the loop (it can be defined there, i.e. getr a new value there), but it can't be declared there.
Last edited by masijade; Nov 20th, 2008 at 9:10 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 20th, 2008
0

Re: Help with Dice Simulation

Okay well I tried moving the declarations for dieRollOne, Two, and Three outside of the loop, and no troubles. But now, when I tried moving the access modifiers outside of the method, it says I need an identifier for myDie = new Random (seedInt);.


public class Rolling
{
private Random myDie;
double seed = Math.random() * 10;
myDie = new Random (seedInt);
int seedInt = (int)seed;
int counter = 0;
int dieRollOne;
int dieRollTwo;
int dieRollThree;
public static void main(String[] args)
{
do
{
dieRollOne = die.nextInt(6) + 1;
dieRollTwo = die.nextInt(6) + 1;
dieRollThree = die.nextInt(6) + 1;
counter += 1;
System.out.println(dieRollOne + " " + dieRollTwo + " " + dieRollThree);
}
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Nov 20th, 2008
0

Re: Help with Dice Simulation

Only the one with the access modifier must be outside of the method, the rest, simply outside of the loop.

And, if you want to define (and not just declare) myDie outside of the method, then you must do it all at one time, not on two separate lines.

But, if it is not going to be static, then you will need to instantiate your class and use it through the instance.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 25th, 2008
0

Re: Help with Dice Simulation

Im new at java, so using these terms doesn't really help me out too much. I mean, you could possibly couple what you mean with relative code, whether it be for my program or just a sample. Thanks...
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Nov 25th, 2008
0

Re: Help with Dice Simulation

:sigh:

Once again, that phrase about a posters knowledge of Java being inverse to the prominence of the word Java in the posters moniker, applies.
Java Syntax (Toggle Plain Text)
  1. public class BogusExample {
  2. public static Random rand = new Random();
  3.  
  4. public static void main (String[] args) {
  5. int one = 1;
  6. int two = 2;
  7. do {
  8. one += two;
  9. two *= rand.nextInt();
  10. } while ((one * 3) < two);
  11. }
  12. }

Java Syntax (Toggle Plain Text)
  1. public class BogusExample {
  2. public Random rand = new Random();
  3.  
  4. public static void main (String[] args) {
  5. Bogus random = new Bogus();
  6.  
  7. int one = 1;
  8. int two = 2;
  9. do {
  10. one += two;
  11. two *= random.rand.nextInt();
  12. } while ((one * 3) < two);
  13. }
  14. }
Last edited by masijade; Nov 25th, 2008 at 9:41 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 25th, 2008
0

Re: Help with Dice Simulation

Im new at java, so using these terms doesn't really help me out too much. .
since this are the appropriate terms, it does help to mention them. that way, you can learn what they mean/are ..
if you stick to java, you're bound to hear them a lot more
Reputation Points: 919
Solved Threads: 352
Nearly a Posting Maven
stultuske is offline Offline
2,471 posts
since Jan 2007
Nov 25th, 2008
0

Re: Help with Dice Simulation

Read this for help with access modifiers. Or just Google it. It's a very common question
Last edited by jasimp; Nov 25th, 2008 at 12:36 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Solve My Question Pls....
Next Thread in Java Forum Timeline: Need help with adt table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC