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.

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.

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.

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.

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);
}

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.

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

: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);
  }
}

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

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

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

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) {

    }
}

I checked all of my {'s.

Seemingly not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.