gpt4 book ai didi

java - 编译错误 : illegal start of expression

转载 作者:搜寻专家 更新时间:2023-11-01 01:23:23 24 4
gpt4 key购买 nike

我正在学习 Java(游戏方面)。我买了一本书,里面有一些我试图复制和测试的代码。唯一的问题是当我尝试编译它时出现错误。

C:\Users\James\Desktop\Java>Javac GamePanel.java                                        
GamePanel.java:57: illegal start of expression
private void gameUpdate()
^
GamePanel.java:57: illegal start of expression
private void gameUpdate()
^
GamePanel.java:57: ';' expected
private void gameUpdate()
^
GamePanel.java:64: reached end of file while parsing
}→
^
4 errors

代码是:

public class GamePanel extends  JPanel implements Runnable
{
private static final int PWIDTH = 500;
private static final int PHEIGHT = 400;

private Thread animator;
private volatile boolean running = false;

private volatile boolean gameOver = false;

public GamePanel()
{
setBackground(Color.white);
setPreferredSize( newDimension(PWIDTH, PHEIGHT));
}

public void addNotify()
{
super.addNotify();
startGame();
}

public void startGame()
{
if (animator == null || !running)
{
animator = new Thread(this);
animator.start();
}
}

public void stopGame()
{
running = false;
}

public void run()
{
running = true;
while(running)
{
gameUpdate();
gameRender();
repaint();

try
{
Thread.sleep(20);
}
catch(InterruptedException ex)
{

}
System.exit(0);
}

private void gameUpdate()
{
if (gameOver == false) {

}
}

}

我知道我可能做错了什么,但我一遍又一遍地检查,有人能告诉我我做错了什么吗?

最佳答案

您缺少 } while 循环未关闭。

public void run()
{
running = true;
while(running)
{
gameUpdate();
gameRender();
repaint();

try
{
Thread.sleep(20);
}
catch(InterruptedException ex)
{

}
} // <<< this is the missing brace
System.exit(0);
}

你可能想要一个类似 eclipse 的 IDE , netbeansintellij (全部免费)并使用它们来格式化您的代码...当您的代码格式正确时,缺少大括号之类的东西会变得更容易找到。

关于java - 编译错误 : illegal start of expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7680528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com