Java 语法错误:插入 } 以完成 ClassBody

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4030032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:41:36  来源:igfitidea点击:

syntax error: insert } to complete ClassBody

javaandroid

提问by LuxuryMode

I created a method and keep getting an error that I need to include a } at the end of my method. I put the } in and the error is still there! If I then delete that } the same error will pop up on a prior method; and that error wasn't there before. in other words, if i type the } on my most recent method then the error stays there and only there. if i delete it, it duplicates that error on my prior method.

我创建了一个方法并不断收到一个错误,我需要在我的方法末尾包含一个 }。我把} 放进去,错误仍然存​​在!如果我然后删除该 } 相同的错误将在先前的方法中弹出;这个错误以前不存在。换句话说,如果我在我最近的方法中输入 } ,那么错误就会停留在那里并且只在那里。如果我删除它,它会在我之前的方法中复制该错误。

private void putThreeBeepers() {
for (int i = 0; i < 2; i++) {
    putBeeper();
    move();
}
putBeeper();
}
private void backUp() {
turnAround();
move();
turnAround();
   }

采纳答案by TofuBeer

You really want to go to the top of your file and do proper and consistent indention all the way to the bottom.

您真的很想转到文件的顶部,并一直到底部进行适当且一致的缩进。

For example...

例如...

private void putThreeBeepers() 
{
    for (int i = 0; i < 2; i++) {
        putBeeper();
        move();
    }

    putBeeper();
}

private void backUp() 
{
    turnAround();
    move();
    turnAround();
}

Odds are, somewhere along the line, you are missing a }. Your description isn't super clear, but if the code you posted is how you actually have it formatted in your file then odds are you just missed something somewhere... and poor indentation makes it very hard to spot.

很可能,沿着这条线的某个地方,你错过了一个}。您的描述不是很清楚,但是如果您发布的代码是您在文件中实际格式化的方式,那么您很可能只是在某处遗漏了某些东西……而且缩进不当使得很难发现。

The fact that the message is changing is confusing, but it is the sort of thing you see in these cases.

消息正在发生变化这一事实令人困惑,但这就是您在这些情况下看到的那种事情。

回答by Jhenry

the error might be misleading. In my case i had incorrect/incomplete comment statements such as below which is broken lead to this error:

该错误可能具有误导性。在我的情况下,我有不正确/不完整的评论语句,例如下面的错误导致此错误:

/*
//  */
*/

Fixing the comments fixed the error. Hope this helps. Thanks.

修复评论修复了错误。希望这可以帮助。谢谢。

回答by TomHanrath

I think this can be caused by a number of different problems. :(

我认为这可能是由许多不同的问题引起的。:(

In my case it was that I have forgotten to specify the type of two parameters in one of my methods declareations. To fix I had to change this: onUpgrade(SQLiteDatabase pDb, pOldVersion, pNewVersion) {

就我而言,我忘记在我的方法声明之一中指定两个参数的类型。为了解决这个问题,我不得不改变这个: onUpgrade(SQLiteDatabase pDb, pOldVersion, pNewVersion) {

} to this: onUpgrade(SQLiteDatabase pDb, int pOldVersion, int pNewVersion)

对此: onUpgrade(SQLiteDatabase pDb, int pOldVersion, int pNewVersion)

回答by Cullen SUN

It might be due to invisible Chars when copying code from PDF ebook. Be careful of the little red dot '.'

这可能是由于从 PDF 电子书中复制代码时字符不可见。小心小红点'.'

Choose 'Select First Character' -> then delete it.

选择“选择第一个字符”-> 然后将其删除。

Invisible Chars when copying code from PDF ebook

从 PDF 电子书中复制代码时出现不可见字符

回答by jwBurnside

Also, the same error might occur if you accidentally write an if-statement outside of a method. I kept overlooking it since I was looking at the bracket-matching only.

此外,如果您不小心在方法之外编写了 if 语句,则可能会发生相同的错误。我一直忽略它,因为我只看括号匹配。

回答by Jane

I just simply add another"}",makes as "}}",then problem solved

我只是简单地添加另一个“}”,变成“}}”,然后问题解决了

I didnt have to put anther "}" for my other java code exercise.

我不必为我的其他 Java 代码练习添加花药“}”。

Im a java beginner,I've come across same problem so I searched online found this thread.Hope this help

我是一个 Java 初学者,我遇到了同样的问题,所以我在网上搜索找到了这个线程。希望这个帮助

回答by Balaji Sankar

Had the same problem. Turned out to be a very fundamental oversight. I was having the properties of a class declared like this:

有同样的问题。结果证明这是一个非常基本的疏忽。我有一个类的属性声明如下:

private Texture foo;
private Sprite bar;
foo = new Texture();
bar = new Sprite();

The mistake was i had been instantiating the foo and bar variables outside the functions of the class. When I put the

错误是我一直在类的函数之外实例化 foo 和 bar 变量。当我把

foo = new Texture();
bar = new Sprite();

into their proper functions (like below), the error went away.

进入其适当的功能(如下所示),错误就消失了。

private Texture foo;
private Sprite bar;
// function
public void instantiateVariables(){
foo = new Texture();
bar = new Sprite();
}

回答by Noumenon

I got this error due to a missing <%.

由于缺少<%.

回答by Dharmendra Pawar

Here are the steps.

以下是步骤。

  1. Just copy paste your code in notepad
  2. Remove copy from Java file
  3. Again copy notepad and paste into Java file.
  4. An error will be gone.
  1. 只需将您的代码复制粘贴到记事本中
  2. 从 Java 文件中删除副本
  3. 再次复制记事本并粘贴到 Java 文件中。
  4. 错误将消失。