Q:

Error with cascading if statement

I have this if then statement I am working on and a part of it seem to not work. In the last part of my class the part that says math.random>.6666 the compiler gives me an error and says that it is not a statement I a not sure what is causing this.
 private static void createShapes() {
     for (int i = 0; i < shapes.length; i++) {
        // Select a random color
        int red = generateRandomInt(0, 255);
        int green = generateRandomInt(0, 255);
        int blue = generateRandomInt(0, 255);
        Color color = new Color(red, green, blue);

        // Decide whether to create a circle or a rectangle
        if (Math.random() > 0&&Math.random()<=.3333333)  {
            // Generate a circle with a random size and position
            int diameter = generateRandomInt(MIN_SIZE, MAX_SIZE);
            int x = generateRandomInt(0, WINDOW_SIZE - diameter);
            int y = generateRandomInt(0, WINDOW_SIZE - diameter);
            shapes[i] = new Circle(x, y, color, diameter);
        } else if (Math.random()>.3333333) {
            // Generate a rectangle with a random size and
            // position
            int width = generateRandomInt(MIN_SIZE, MAX_SIZE);
            int height = generateRandomInt(MIN_SIZE, MAX_SIZE);
            int x = generateRandomInt(0, WINDOW_SIZE - width);
            int y = generateRandomInt(0, WINDOW_SIZE - height);
            shapes[i] = new Rectangle(x, y, color, width, height);      
        } else   (Math.random()>.66666666666){
            int leng = generateRandomInt(MIN_SIZE, MAX_SIZE);
            int x = generateRandomInt(0, WINDOW_SIZE - leng);
            int y = generateRandomInt(0, WINDOW_SIZE - leng);
            shapes[i] = new Triangle(x, y, color, leng);                      
        }
    }
}

A:

You're creating a new random number for every if statement.  You should create one variable.  In your current code, even after the else if fix, all those conditions could possibly be met, or even none at all.
 double random = Math.random();     

 if (random > 0 && random <=.3333333)  {
     // Generate a circle with a random size and position
     int diameter = generateRandomInt(MIN_SIZE, MAX_SIZE);
     int x = generateRandomInt(0, WINDOW_SIZE - diameter);
     int y = generateRandomInt(0, WINDOW_SIZE - diameter);
     shapes[i] = new Circle(x, y, color, diameter);
 } else if (random >.3333333 && random < 0.666666) {
     // Generate a rectangle with a random size and
     // position
     int width = generateRandomInt(MIN_SIZE, MAX_SIZE);
     int height = generateRandomInt(MIN_SIZE, MAX_SIZE);
     int x = generateRandomInt(0, WINDOW_SIZE - width);
     int y = generateRandomInt(0, WINDOW_SIZE - height);
     shapes[i] = new Rectangle(x, y, color, width, height);

 } else {
     int leng = generateRandomInt(MIN_SIZE, MAX_SIZE);
     int x = generateRandomInt(0, WINDOW_SIZE - leng);
     int y = generateRandomInt(0, WINDOW_SIZE - leng);
     shapes[i] = new Triangle(x, y, color, leng);                     
 }

You don't even need the last condition in the else, because if the other two conditions are not met, the last will automatically be met. All you need is else.
An even better design would me
if (random > 0.66666) {
   ...
}
else if (random > 0.33333){
   ...
}
else {
   ...
}

If the first condition is met, the other two won't perform. If the first condition is not met, and the second is met, the third will not perform. If the first two are not met, the third will perform.

