1,105,208 Community Members

Help with Dice Simulation

Member Avatar
IMtheBESTatJAVA
Light Poster
36 posts since Oct 2008
Reputation Points: -11 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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.

Member Avatar
IMtheBESTatJAVA
Light Poster
36 posts since Oct 2008
Reputation Points: -11 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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.

Member Avatar
IMtheBESTatJAVA
Light Poster
36 posts since Oct 2008
Reputation Points: -11 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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);
}

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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.

Member Avatar
IMtheBESTatJAVA
Light Poster
36 posts since Oct 2008
Reputation Points: -11 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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

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);
  }
}
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);
  }
}
Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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

Member Avatar
jasimp
Senior Poster
3,593 posts since Aug 2007
Reputation Points: 427 [?]
Q&As Helped to Solve: 99 [?]
Skill Endorsements: 0 [?]
Featured
 
0
 

Read this for help with access modifiers. Or just Google it. It's a very common question

Member Avatar
IMtheBESTatJAVA
Light Poster
36 posts since Oct 2008
Reputation Points: -11 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hmmm... well, I'm getting an error on my code on the line
public static void main(String[] args)
and I checked all of my {'s. The error says "class, interface, or enum expected".

public class Rolling
{   
    private random myDie;
} 
 public static void main(String[] args)
  {
    double seed = Math.random ( ) * 10;
    int seedInt = (int)seed;
    myDie = new Random (seedInt);
    int counter = 0;
    int dieRollOne;
    int dieRollTwo;
    int dieRollThree;
    
    do
    {
    dieRollOne = myDie.nextInt(6) + 1;
    dieRollTwo = myDie.nextInt(6) + 1;
    dieRollThree = myDie.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);
}
    }

Any help appreciated...

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

This:

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

}

Should be:

public class Rolling
{   
    private random myDie;

    public static void main(String[] args) {

    }
}
Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

I checked all of my {'s.

Seemingly not.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article