Java 括号放置错误
我收到以下错误:标记}上的语法错误,{应在该标记之后 我不确定这里的问题是什么,我已经检查了括号是否匹配,或者我认为是这样,所以我不完全清楚如何解决这个问题Java 括号放置错误,java,Java,我收到以下错误:标记}上的语法错误,{应在该标记之后 我不确定这里的问题是什么,我已经检查了括号是否匹配,或者我认为是这样,所以我不完全清楚如何解决这个问题 import java.awt.*; public class Player extends Entity { private int directionX, directionY; private Main instance; private Rectangle hitbox; private
import java.awt.*;
public class Player extends Entity {
private int directionX, directionY;
private Main instance;
private Rectangle hitbox;
private int life = 3;
public Player(Main instance, int x, int y){
super(x , y);
this.instance = instance;
width = 16; height = 16;
hitbox = new Rectangle(x, y, width, height);
}
public void draw(Graphics g){
move();
g.setColor(Color.WHITE);
g.fillOval(hitbox.x, hitbox.y, hitbox.width, hitbox.height);
g.setColor(Color.WHITE);
g.drawString("Lives: " + life, 20, 20);
}
private void move(){
if(!instance.getStage().isCollided(hitbox)){
directionY = 1;
} else { directionY = 0;
hitbox.x += directionX;
hitbox.y += directionY;
}
} // <<<<<< Getting error here <<<<<<<<<<
if(instance.getEnemyManager().isCollided(hitbox)){
if(life > 0){
life--;
hitbox.x = 800 / 2 - width / 2;
y = 390;
} else {
instance.setGameOver(true);
}
}
public void setdirectionX(int value){
directionX = value;
}
public void setdirectionY(int value){
directionY = value;
}
}
如果有人能回答这个问题,我很高兴知道。请查看问题区域中的代码:
private void move(){
if(!instance.getStage().isCollided(hitbox)){
directionY = 1;
} else { directionY = 0;
hitbox.x += directionX;
hitbox.y += directionY;
}
}
if(instance.getEnemyManager().isCollided(hitbox)){
关闭括号是方法的结尾…然后你得到了一个if语句,它在类声明的中间,而不是在任何方法中。你打算把它作为移动的一部分吗?如果是的话,你需要在它之前删除关闭括号。
请注意,至少在你的帖子中,缩进到处都是。如果你让IDE格式化你的代码,这样你就可以看到什么代码是什么方法的一部分,等等。这也让其他人更容易阅读…if(instance.getEnemyManager().isCollided(hitbox)){
if(life > 0){
life--;
hitbox.x = 800 / 2 - width / 2;
y = 390;
} else {
instance.setGameOver(true);
}
}
在任何方法之外,这就是编译器警告您的原因