0

我有这个问题,它一直抛出非法的表达式开头,我检查了括号,我认为它们很好。这是代码:另外,它说第 30 行有一个错误,我不太明白....

    public class Menu extends BasicGameState {

Image playNow;
Image exitGame;

public String mouse = "No Input Yet!";

public Menu(int state) {
}

@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    playNow = new Image("res/playNow.png");
    exitGame = new Image("res/exitGame.png");
}

@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    g.drawString(mouse, 590, 10);
    g.drawString("Welcome to my Game!", 100, 50);
    playNow.draw(100,100);
    exitGame.draw(100, 200);
}
//slick counts from the bottom-left of the display, not the top-left, like java does

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    Input input = gc.getInput();
    int mousex = Mouse.getX();
    int mousey = Mouse.getY();
    mouse = "Mouse coordinate x: " + mousex + " y: " + mousey;
    // x-min:105  x-max:300  y-min:  y-max:300
    if(input.isMouseButtonDown(0)) {
        if(mousex>100 && mousex<300) && (mousey>400 && mousey< 435) { //error
        sbg.enterState(1);
    }
        if(mousex>100 && mousex<300) && (mousey>300 && mousey <335) { //error
        System.exit(0);
    }
    }
}

@Override
public int getID() {
    return 0;
}

} 我真的需要快速帮助。

4

1 回答 1

1

在 Java 中,an 的条件if必须完全用括号括起来。改变

if(mousex>100 && mousex<300) && (mousey>400 && mousey< 435) {

if((mousex>100 && mousex<300) && (mousey>400 && mousey< 435)) {

...和其他if条件类似。

编译器认为这(mousex>100 && mousex<300)是整个条件,&& (mousey>400 && mousey< 435)作为条件的主体没有意义。

于 2013-10-31T22:38:01.873 回答