繁体   English   中英

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

[英]Why won't my code compile?

我想弄清楚为什么这段代码没有正确编译。 当我尝试编译时,我得到它需要一个 main 的错误,当我添加 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


    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM