【问题标题】:Where to Add Main Method Java在哪里添加 Main 方法 Java
【发布时间】:2017-02-13 09:47:28
【问题描述】:

我有这个代码块,它需要一个 main 方法才能运行。

public class Point {

    private int xcoord;
    private int ycoord;

    public Point () {}

    public Point (int x, int y) {}

    public int getX () {
        return xcoord;
    }

    public int getY () {
        return ycoord;
    }

    public void moveUp(int amount) {}

    public void moveDown(int amount) {}

    public void moveRight(int amount) {}

    public void moveLeft(int amount) {}

}

我尝试在public class Point 下方添加public static void main(String[] args){,但这会导致整个程序出现问题(说Public Point () 需要声明为新的)并且我关闭了MoveLeft 方法关闭下的main 方法冒号和public class 点结束冒号,像这样:

public class Point {

    private int xcoord;
    private int ycoord;

    public static void main(String[] args){

        public Point () {}

        public Point (int x, int y) {}

        public int getX () {
            return xcoord;
        }

        public int getY () {
            return ycoord;
        }

        public void moveUp(int amount) {}

        public void moveDown(int amount) {}

        public void moveRight(int amount) {}

        public void moveLeft(int amount) {}
    }

}

【问题讨论】:

  • 您的基本语法错误。 Main 是一个方法,它不应该包含其他方法。只需添加 main 就像您在第二个代码示例中的类中添加 moveUp 方法一样。

标签: java methods main


【解决方案1】:

Main 是一个方法,您不希望其他方法包含在其中。

试试这样的:

public class Point {

private int xcoord;
private int ycoord;

public static void main(String[] args) {

} //End of main

   //Now you add the rest of your methods
    public Point(){

    }

//And so on

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2020-04-12
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多