级联if语句时出错

时间:2013-11-16 17:40:17

标签: java

我有这个,如果我正在处理的声明,其中一部分似乎不起作用。在我班级的最后一部分,数学上写着math.random> .6666的编译器给了我一个错误,并说它不是一个声明,我不确定是什么导致了这个。

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

4 个答案:

答案 0 :(得分:2)

您有else (Math.random()>.66666666666){

那不会编译。您需要一个IF,或者您需要删除对Math.Random()的检查。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您正在为每个if语句创建一个新的随机数。你应该创建一个变量。在您当前的代码中,即使在else if修复之后,所有这些条件都可能得到满足,甚至根本没有。

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

您甚至不需要else中的最后一个条件,因为如果不满足其他两个条件,则会自动满足最后一个条件。您所需要的只是else

我会有更好的设计

if (random > 0.66666) {
   ...
}
else if (random > 0.33333){
   ...
}
else {
   ...
}

如果满足第一个条件,则其他两个条件不会执行。如果第一个条件不满足,第二个条件满足,则第三个条件不会执行。如果不满足前两个,则第三个将执行。

答案 3 :(得分:0)

您可以使用已编写的代码来使其更清晰。我假设你的generateRandomInt方法在它的两个参数之间产生一个数字,包括(也就是说,两个参数都是可能的返回值)。

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