编译游戏代码时出现异常错误

时间:2013-10-28 14:40:48

标签: java exception

我是Java的初学者,我尝试运行以下代码:

package Happily.Insane.Rain;


import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
        private static final long serialVersionUID = 1L;

        public static int width = 300;
        public static height = width / 16 * 9;
        public static int scale = 3;

        private Thread thread;
        private JFrame frame;
        private boolean running = false;

        public Game() {
                Dimension size = new Dimension (width*scale, height*scale);
                setPreferredSize(size);

                frame = new JFrame();
        }

        public synchronized void start() {
                running = true;
                thread = new Thread (this, "Display");
                thread.start();
        }

        public synchronized void stop() {
                running = false;
                try {
                        thread.join();
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
        }

        public void run() {
                while (running) {
                        System.out.println("Running...");
                }
        }

        public static void main(String[] args) {

                Game game = new Game();
                game.frame.setResizable(false);
                game.frame.setTitle("Rain");
                game.frame.add(game);
                game.frame.pack();
                game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                game.frame.setLocationRelativeTo(null);
                game.frame.setVisible(true);

                game.start();
        }

}

有错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

        at Happily.Insane.Rain.Game.main(Game.java:47)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

您没有指定静态变量height的类型:

public static height = width / 16 * 9;
// ----------^

它应该是一个整数:

public static int height = width / 16 * 9;

在以后的帖子中,请突出显示受影响的行。此外,我们不是一个给予我们的问题 - 我们将解决他们所有的系统。投入一些自己的努力,做一些研究,然后来找我们问一个问题。