简体   繁体   中英

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

You have else (Math.random()>.66666666666){

That won't compile. Either you need an IF, or you need to remove the check on Math.Random().

您在最后一个之后缺少if语句:

else if  (Math.random()>.66666666666){

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.

You can use code you've already written to make this much cleaner. I'm assuming that your generateRandomInt method produces a number between its two parameters, inclusively (that is, both parameters are possible return values).

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);
        int shapeType = generateRandomInt(0, 2);

        switch (shapeType) {

        case 0:
            // 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);
            break;

        case 1:
            // 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);      
            break;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM