| | |
Help with Dice Simulation
![]() |
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
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.
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.
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
----------------------------------------------
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
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.
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
----------------------------------------------
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
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
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);
}
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);
}
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.
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
----------------------------------------------
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
: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.
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)
public class BogusExample { public static Random rand = new Random(); public static void main (String[] args) { int one = 1; int two = 2; do { one += two; two *= rand.nextInt(); } while ((one * 3) < two); } }
Java Syntax (Toggle Plain Text)
public class BogusExample { public Random rand = new Random(); public static void main (String[] args) { Bogus random = new Bogus(); int one = 1; int two = 2; do { one += two; two *= random.rand.nextInt(); } while ((one * 3) < two); } }
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
----------------------------------------------
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
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
![]() |
Similar Threads
- dice simulation (Python)
- Round Robin Scheduler (C)
- Need Help Printing Histogram Asterisks (Java)
Other Threads in the Java Forum
- Previous Thread: Solve My Question Pls....
- Next Thread: Need help with adt table
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






