编译错误:非法启动表达式 [英] Compile Error: illegal start of expression
问题描述
我正在学习Java(游戏方面)。我买了一本书,它有一些代码,我试着复制并测试它。唯一的问题是,当我尝试编译它时会出现错误。
I'm learning Java (The Gaming Side). I bought a book and it has some code in which I tried to copy and test it. The only problem is that it comes up with errors when I try to compile it.
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) {
}
}
}
我知道我可能做错了但我查了一下一遍又一遍,有人可以告诉我我做错了什么吗?
I know I'm probably doing something wrong but I checked it over and over again, can someone please enlighten me on what I am doing wrong?
推荐答案
你错过了}
while循环未关闭。
Your missing a }
the while loop isn't closed.
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 , netbeans 或 intellij (全部免费)并使用它们来格式化你的代码...当代码格式正确时,丢失大括号等内容会变得更容易找到。
You might want to get an IDE like eclipse, netbeans or intellij (all free) and use them to format your code...things like missing braces become a lot easier to find when your code is correctly formatted.
这篇关于编译错误:非法启动表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!