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

Java 我得到';其他';没有';如果';错误,java,Java,我得到的“else”没有“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()

我得到的“else”没有“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;
}

}
class-MyGraphics
{
帧缓冲区fb;
int x,y;
私人int宽度、高度;
MyGraphics(int-wid,int-hit)
{
fb=新帧缓冲区(wid,hit);
宽度=fb.getWidth();
高度=fb.getHeight();
}
MyGraphics()
{
fb=新帧缓冲区();
宽度=fb.getWidth();
高度=fb.getHeight();
}
空心抽绳(int x1、int y1、int x2、int y2){
int x0;
双y0=y1;
双斜率=(双)(y2-y1)/(双)(x2-x1);
if(x2<宽度和y2<高度)
{
对于(x0=x1;x0=高度)
{
对于(y0=y1,x0=x1;(int)y0如果(x2>=width&&y2在初始if之后缺少一个结束括号

但是你的编译器应该告诉你应该在哪里查找

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

问题在于:

if (x2 < width && y2 < height)
{
  // if body
}
fb.setPixel(x0, ((int)y0)); 
else if(x2 < width && y2 >= height) // this else has no matching if.
if(x2<宽度和&y2<高度)
{
//如果主体
}
fb.setPixel(x0,((int)y0));
else if(x2=height)//此else没有匹配的if。

您是否检查了宽度/高度的值,以及您的代码是否编译?如果在if之后缺少一个
,这就是正确缩进后代码的外观:

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));
}
if(x2<宽度和&y2<高度)
{
对于(x0=x1;x0=高度)
{
对于(y0=y1,x0=x1;(int)y0=width&&y2您指定的错误(
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(x2

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

问题由其他人解决

以下是我的两条建议:

  • 使用“tab”使您的代码看起来更好
  • 当涉及到括号问题时,总是先键入“{”和“}”,然后在它们之间添加内容。这样你就再也不会出现这种错误了
  • 这些是我的方法,如果有用的话,你可以使用它们


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

    如果您能清楚正确地缩进代码,这将是非常明显的。您缺少了一个}。请正确缩进您的代码,您就会知道问题所在。为什么会出现所有的否决票?我们不会都错的…安装一个IDE,您就不会有这样的问题。我个人喜欢Eclipse,我同意Jordan的看法。尝试使用java开发人员使用的任何一个IDE,如Eclipse/Intellij/Netbeans。您可以通过您的帮助解决所有错误在IDE的帮助下,您自己。是否有人认为那些
    s确实有匹配的
    if
    。。。?