Java 代码无法运行,出现奇怪错误

Java 代码无法运行,出现奇怪错误,java,Java,我有一些代码,除了出现一个错误外,所有代码似乎都正常工作。这将停止整个程序的运行。请你看一下代码,看看问题出在哪里。这是一个空间入侵者游戏,这个类包含公共静态void main import java.awt.*; public class Alien { public static void main(String[] args) { // TODO Auto-generated method stub /** * The Alien

我有一些代码,除了出现一个错误外,所有代码似乎都正常工作。这将停止整个程序的运行。请你看一下代码,看看问题出在哪里。这是一个空间入侵者游戏,这个类包含公共静态void main

import java.awt.*;
public class Alien {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        /**
         * The Alien class. 
         */


            public static int ALIEN_HEIGHT = 25;
            public static int ALIEN_WIDTH = 15;

            private int leftPosition = 0;
            private int heightPosition = 0;

            private boolean hitState = false;//Whether this alien has already been shot

            private Image alienImage = null;

            SpaceInvaders spaceInvaders = null;

            /**
             *
             */
            public Alien(Image ai, SpaceInvaders si) {
                alienImage = ai;
                spaceInvaders = si;
            }

            /**
             * Returns whether ythe alien had been hit
             */
            public boolean hasBeenHit() {
                return hitState;
            }

            /**
             * Check if a shot fired hit an alien
             */
            public boolean hitAlien(int x, int y) {

                //Is the alien currently alive?
                if (hitState) {
                    //If it's alreay been shot then return false;
                    return false;
                }

                //First lets check the X range
                if ((x >= leftPosition) && (x <= (leftPosition+ALIEN_WIDTH))) {
                    //X is ok, now lets check the Y range
                    if ((y >= heightPosition) && (y <= (heightPosition+ALIEN_HEIGHT))) {
                        //We shot an alien!
                        hitState = true;
                        return true;
                    }
                } 
                return false;
            }

            /**
             * Set the position of the alien on the screen
             */
            public void setPosition(int x, int y) {
                leftPosition = x;
                heightPosition = y;
            }

            /**
             * Returns the current x position of the alien
             */
            public int getXPos() {
                return leftPosition;
            }

            /**
             * Returns the current x position of the alien
             */
            public int getYPos() {
                return heightPosition;
            }

            /**
             * Draw the image of the Alien 
             */ 
            public void drawAlien(Graphics g) {
                if (!hitState) {
                    g.setColor(Color.red);
                    g.fillRect(leftPosition, heightPosition, ALIEN_WIDTH, ALIEN_HEIGHT);
                }
            }

        }
    }

}

您似乎误解了Java语法的基础知识。具体来说,您不能在
main()
中定义类的成员和方法

import java.awt.*;
public class Alien {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        /**
         * The Alien class. 
         */


            public static int ALIEN_HEIGHT = 25;  // you can't put this here
            public static int ALIEN_WIDTH = 15;   // you can't put this here

            private int leftPosition = 0;         // you can't put this here
            private int heightPosition = 0;       // you can't put this here
            //etc
要编译此文件,您需要关闭
main()
,并删除代码末尾的所有额外
}

import java.awt.*;

public class Alien {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }

    /**
     * The Alien class. 
     */
    public static int ALIEN_HEIGHT = 25;
    public static int ALIEN_WIDTH = 15;
    //etc
在程序实际执行任何操作之前,还需要向
main()
添加一些代码

我建议您在继续当前课程之前,先阅读一些优秀的在线教程。oracle网站是一个很好的起点


查看涵盖基础知识的小径部分,在做任何其他事情之前,先完成这一部分。

。。。那个“奇怪”的错误是。。。请发布错误消息的全文,我们无法猜测。抱歉,它[看起来]怎么能工作?它甚至没有编译?这意味着你试图运行不可编译的代码--不要这样做。首先修复编译问题,先测试它是否编译,然后再尝试运行它。1+看起来OP需要先阅读Java书籍,然后再尝试创建程序。他似乎只是在猜测这一点,并希望它能以某种方式起作用,而这种方法永远不会起作用。
import java.awt.*;

public class Alien {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }

    /**
     * The Alien class. 
     */
    public static int ALIEN_HEIGHT = 25;
    public static int ALIEN_WIDTH = 15;
    //etc