【问题标题】:Illegal start of expression java slick表达式java slick的非法开始
【发布时间】:2013-10-31 22:36:41
【问题描述】:

我有这个问题,一直抛出非法的表达式开头,我检查了括号,我认为它们很好。这是代码:另外,它说第 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;
}

} 我真的急需帮助。

【问题讨论】:

    标签: java expression slick


    【解决方案1】:

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

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

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

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

    编译器认为(mousex&gt;100 &amp;&amp; mousex&lt;300) 是整个条件,而&amp;&amp; (mousey&gt;400 &amp;&amp; mousey&lt; 435) 作为条件的主体没有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-11
      • 2021-12-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 2015-05-30
      相关资源
      最近更新 更多