java - 如何消除此消息错误 : Main method not found in class

标签 java program-entry-point

我最近一直在开发这个 Java 游戏。我试图用音频剪辑添加音乐,但它不起作用,所以我取出了主文件中的代码并删除了主文件,因为我不再需要它了。所有代码与我实现音乐之前相同,但现在弹出此消息并且不允许我运行我的游戏:

Error: Main method not found in class com.illuminationsco.GoNerdGo.Entities.Obstacles, please define the main method as: public static void main(String[] args)

当我尝试放回主程序时,它根本不运行!

如果有人能帮助我那就太好了!

这是我的代码:

Bully bully = new Bully();
Nerd nerd = new Nerd();

static BufferedImage[] sprites;

public static float x, y, velX = 5.5f, velY = 5.5f;
public static int whatObstacle = 5;

public static int basketBall = 6;
public static int lava = 2;
public static int trash = 1;

public static boolean trashOpen = false;

public Rectangle getBounds() {
    return null;
}

public Obstacles() {
    int width = 100;
    int height = 100;
    int columns = 2;
    int rows = 2;

    BufferedImage spriteSheet = null;
    try {
        spriteSheet = ImageIO.read(new File(
                "res/Images/ObstacleSpriteSheet.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    sprites = new BufferedImage[rows * columns];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            sprites[(i * columns) + j] = spriteSheet.getSubimage(i * width,
                    j * height, width, height);
        }
    }
}

public void tick() {
    if (whatObstacle >= basketBall && Game.gos == null) {
        x += velX;
        y += velY;
    }

    if (x <= 0 || x >= 800 - 35) {
        velX *= -1;
    }
    if (y <= 425 || y >= 600 - 35) {
        velY *= -1;
    }
}

public void render(Graphics g) {
    if (whatObstacle >= basketBall) {

        // Displaying basketball

        g.drawImage(sprites[3], (int) x, (int) y, null);

        Rectangle basketballRect = new Rectangle((int) x, (int) y, 35, 35);

        if (nerd.getFeetBounds().intersects(basketballRect)) {
            Game.setOverGameState(Game.GameOverlayState.BullyWins);
        }
        if (bully.getFeetBounds().intersects(basketballRect)) {
            Game.setOverGameState(Game.GameOverlayState.NerdWins);
        }

        // Displaying lava pool
    } else if (whatObstacle >= lava) {

        g.drawImage(sprites[2], (int) x, (int) y, null);

        Rectangle lavaRect = new Rectangle((int) x + 5, (int) y + 7, 80, 19);

        if (nerd.getFeetBounds().intersects(lavaRect)) {
            Game.setOverGameState(Game.GameOverlayState.BullyWins);
        }
        if (bully.getFeetBounds().intersects(lavaRect)) {
            Game.setOverGameState(Game.GameOverlayState.NerdWins);
        }

        // Displaying trash can

    } else if (whatObstacle == trash) {

        if (trashOpen) {
            g.drawImage(sprites[1], (int) x, (int) y, null);
        } else {
            g.drawImage(sprites[0], (int) x, (int) y, null);
        }

        Rectangle trashRect = new Rectangle((int) x, (int) y, 60, 97);

        if (bully.getBounds().intersects(trashRect)) {
            Game.setOverGameState(Game.GameOverlayState.BullyWins);
            Obstacles.trashOpen = true;
        }
    }
}

最佳答案

在你的类中插入一个主方法 - public static void main(String[] args)! 当您运行 Java 程序时,输出只是此方法中的输出。所以你总是需要它!

关于java - 如何消除此消息错误 : Main method not found in class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26565214/

相关文章:

Java Puzzler-这是什么原因?

java - 访问 obb 文件时出错

分数高于 100 时无法显示 "Invalid Score. Please Re-enter"

java - 使用 int 而不是 String : public static void main (int[] args)

c# - 如何制作具有多个项目和不同输出的安装程序包?

c - main() 函数与 C 中的其他函数相比如何?

java - 我应该在哪里声明一个类中的变量以便在另一个类中使用?

java - 将 GWTquery 与 GWT 结合使用

Java - 在具有 Class<? 的泛型类上使用 newInstance()扩展我的类> : InstantiationException

java - 如何从 Java 中的方法返回通用 Map