为什么我的代码不能编译?

我想弄清楚为什么这段代码没有正确编译。 当我尝试编译时,我得到它需要一个主要的错误,当我添加main时,它会导致更多的错误,我真的不知道如何解决。 有人可以看看代码并帮助我吗? 如果有人可以帮助它,将不胜感激。

public class Polygon {

private int numSides; //number of sides
private double sideLength; //length of each side
private double xCoord; //x-coordinate of th center of the polygon
private double yCoord;//the y-coordinate
private double apothem;//defines the apothem
private double perimeter;

/**
 * no argument constructor
 */
public Polygon() {
    this.numSides = 4;
    this.sideLength = 10.0;
    this.xCoord = 0.0;
    this.yCoord = 0.0;
    this.apothem = 5.0;
    this.perimeter = 20.0;
}

/**
 * constructor that takes arguments
 *
 * @param _numSides :-number of sides
 * @param _sideLength :-the length of each side
 * @param _xCoord :-the x coordinate
 * @param _yCoord :-the Y coordinate
 * @param _apothem :-the apothem
 */
public Polygon(int _numSides, double _sideLength, double _xCoord, double _yCoord, double _apothem) {

    this.numSides = _numSides;
    this.sideLength = _sideLength;
    this.xCoord = _xCoord;
    this.yCoord = _yCoord;
    this.apothem = _apothem;

}

/**
 *
 * @return area of the polygon[double]
 */
public double getArea() {
    perimeter = numSides * sideLength;
    double area = (0.5) * apothem * perimeter;
    return area;

}
//getter & setters

public int getNumSides() {
    return numSides;
}

public void setNumSides(int numSides) {
    this.numSides = numSides;
}

public double getSideLength() {
    return sideLength;
}

public void setSideLength(double sideLength) {
    this.sideLength = sideLength;
}

public double getxCoord() {
    return xCoord;
}

public void setxCoord(double xCoord) {
    this.xCoord = xCoord;
}

public double getyCoord() {
    return yCoord;
}

public void setyCoord(double yCoord) {
    this.yCoord = yCoord;
}

public double getApothem() {
    return apothem;
}

public void setApothem(double apothem) {
    this.apothem = apothem;
}

public double getPerimeter() {
    return perimeter;
}

public void setPerimeter(double perimeter) {
    this.perimeter = perimeter;
}

//to string method
@Override
public String toString() {
    return "Polygon definitions[" + "number of sides=" + numSides + ", Each side length=" + sideLength + ", xCoord=" + xCoord + ", yCoord=" + yCoord + ", apothem=" + apothem + ']';
}

}

我很抱歉未事先包括错误。 当通过cmd编译时,我得到以下错误。

错误:在类Polygon中找不到Main方法,请将主方法定义为:public static void main(String [] args)或者JavaFX应用程序类必须扩展javafx.application.Application


在同一个包中创建一个名为Main的单独类。 在那里添加主要方法。 然后实例化一个对象。

public class Main {

    public static void main(String[] args) {

        Polygon p = new Polygon();
        double apothem = p.getApothem();
        System.out.println(apothem);
    }
}

你的代码编译得很好。 你所缺乏的是另一个使用你刚创建的对象类的类。

所以基本上你需要做的是去你的编辑器(eclipse,notepad ++等),创建另一个文件并使用如下所示:

public class PolygonMain {

    public static void main(String[] args) {

       Polygon p = new Polygon();

       Here you can use your getters/setters toString etc. 
       The name of the object you've created in this example would be p, therefore
        p.toString() would return the toString method of your object class


    }

}
链接地址: http://www.djcxy.com/p/73647.html

上一篇: Why won't my code compile?

下一篇: How to convert a char to a String?