简体   繁体   English

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

[英]Why won't my code compile?

I'm trying to figure out why this code isn't compiling correctly.我想弄清楚为什么这段代码没有正确编译。 When I try to compile, I get the error that it needs a main, when I add the main, it leads to a ton more errors that I'm honestly not sure how to fix.当我尝试编译时,我得到它需要一个 main 的错误,当我添加 main 时,它会导致更多的错误,我真的不知道如何修复。 Can someone take a look at the code and help me out?有人可以看看代码并帮助我吗? If anyone could help it would be greatly appreciated.如果有人可以提供帮助,将不胜感激。

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 + ']';
}

} }

I apologize for not including the errors beforehand.我很抱歉没有事先包含错误。 When compiling via cmd I get the following error.通过 cmd 编译时出现以下错误。

Error: Main method not found in class Polygon, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application错误:在类 Polygon 中找不到 Main 方法,请将主要方法定义为:public static void main(String[] args) 或 JavaFX 应用程序类必须扩展 javafx.application.Application

Create a separate class called Main in the same package.在同一个包中创建一个名为 Main 的单独类。 Add the main method there.在那里添加主要方法。 then instantiate an object.然后实例化一个对象。

public class Main {

    public static void main(String[] args) {

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

Your code is compiling just fine.你的代码编译得很好。 What you're lacking is another class that uses the object class that you've just created.您缺少的是另一个使用您刚刚创建的对象类的类。

So basically what you need to do is go to your editor (eclipse, notepad++, etc.), make another file and use something like this:所以基本上你需要做的是去你的编辑器(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