我得到'其他'没有'如果'错误

时间:2011-11-16 04:39:35

标签: java

我没有'if'错误就得'其他'。 请帮忙,谢谢

class MyGraphics
{
    FrameBuffer fb;
    int x, y;
    private int width, height;
    MyGraphics(int wid, int hit)
    {
        fb= new FrameBuffer(wid,hit);
        width = fb.getWidth();
        height = fb.getHeight();
    }
    MyGraphics()
    {
        fb = new FrameBuffer();
        width = fb.getWidth();
        height = fb.getHeight();
    }


    void drawLine(int x1, int y1, int x2, int y2){
    int x0;
    double y0 = y1;
    double slope = (double)(y2 - y1) / (double)(x2 - x1);

    if (x2 < width && y2 < height)
    {
        for (x0 = x1; x0<=x2; x0++)

        y0=y0+ slope;
    }

    fb.setPixel(x0, ((int)y0));

    else if(x2 < width && y2 >= height)
    {
        for( y0=y1,x0=x1;  (int)y0< height;   x0++)
            y0 = y0 + slope;
    }
    fb.setPixel(x0, ((int)y0));

    else if(x2 >= width && y2 <height)
    {
        for (x0 = x1; x0 < width; x0++)
        y0=y0+ slope;
    }
    fb.setPixel(x0, ((int)y0));

    else
    {
        for(x0=x1; x0 < width && (int)y0 < height;x0++)
            y0 = y0 + slope;
        fb.setPixel(x0, ((int)y0));
    }

    return;
}
void display()
{
    fb.display();
    return;
}

}

6 个答案:

答案 0 :(得分:2)

这是您的代码在正确缩进后的样子:

if (x2 < width && y2 < height)
{
  for (x0 = x1; x0<=x2; x0++)
      y0=y0+ slope;
}
fb.setPixel(x0, ((int)y0));

/* THIS ELSE HAS NO MATCHING IF */
else if(x2 < width && y2 >= height)
{
    for( y0=y1,x0=x1;  (int)y0< height;   x0++)
        y0 = y0 + slope;
}
fb.setPixel(x0, ((int)y0));

/* NEITHER DOES THIS ONE */
else if(x2 >= width && y2 <height)
{
    for (x0 = x1; x0 < width; x0++)
    y0=y0+ slope;
}
fb.setPixel(x0, ((int)y0));

/* NOR THIS ONE */
else
{
    for(x0=x1; x0 < width && (int)y0 < height;x0++)
        y0 = y0 + slope;
    fb.setPixel(x0, ((int)y0));
}

答案 1 :(得分:1)

在初始if ...

之后你错过了一个结束括号

但是你的编译器应该告诉你确切的位置。

您有多个支架问题...如果您使用的是IDE,请尝试使用它格式化代码,或者至少突出显示匹配的括号。这应该有助于缩小问题范围。

答案 2 :(得分:1)

问题在于:

if (x2 < width && y2 < height)
{
  // if body
}
fb.setPixel(x0, ((int)y0)); 
else if(x2 < width && y2 >= height) // this else has no matching if.

答案 3 :(得分:1)

您指定的错误(else没有if)完全正确。看看你的代码:

if (x2 < width && y2 < height) {   
  for (x0 = x1; x0<=x2; x0++)

    y0=y0+ slope;
    }

    fb.setPixel(x0, ((int)y0));

else if(x2 < width && y2 >= height)

此处,您已在if之后结束了y0 = y0 + slope语句,之后又结束了fb.setPixel。当你到达else if时,没有匹配的if,因为你已经结束它并在它之后执行了语句。

答案 4 :(得分:1)

其他人解决了问题。

这是我的2条建议:

  1. 使用“tab”使您的代码更好看。
  2. 当涉及括号问题时,请先输入“{”和“}”,然后在它们之间添加内容。然后你再也不会遇到这种错误了。
  3. 这些是我的方式,如果有用,你可以使用它们。

    更重要的是,您可以使用IDE。事情会变得更好。大多数IDE的默认设置都足够了。

答案 5 :(得分:0)

您是否检查了宽度/高度的值并且您的代码是否已编译?如果

,您错过了}