Help with Dice Simulation

Reply

Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

Help with Dice Simulation

 
0
  #1
Nov 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,357
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Help with Dice Simulation

 
0
  #2
Nov 20th, 2008
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

Re: Help with Dice Simulation

 
0
  #3
Nov 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,357
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Help with Dice Simulation

 
0
  #4
Nov 20th, 2008
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

Re: Help with Dice Simulation

 
0
  #5
Nov 20th, 2008
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);
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,357
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Help with Dice Simulation

 
0
  #6
Nov 20th, 2008
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

Re: Help with Dice Simulation

 
0
  #7
Nov 25th, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,357
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Help with Dice Simulation

 
0
  #8
Nov 25th, 2008
: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.
  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. }

  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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Help with Dice Simulation

 
0
  #9
Nov 25th, 2008
Originally Posted by IMtheBESTatJAVA View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 51
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Help with Dice Simulation

 
0
  #10
Nov 25th, 2008
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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC