2016-09-18 120 views
-2

我想弄清楚爲什麼這段代碼編譯不正確。當我嘗試編譯時,我得到它需要一個主要的錯誤,當我添加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編譯時,我得到以下錯誤。

錯誤:在類的多邊形沒有找到主方法,請定義main方法爲:公共靜態無效的主要(字串[] args) 或JavaFX應用程序類必須擴展javafx.application.Application

+0

「當我添加的主要,它會導致一噸更多的錯誤「< - 也許你可以開始分享代碼和你看到的錯誤。 – smarx

+0

基本SO建議:不要總結錯誤消息,將其粘貼到您的問題中。你說你得到一個錯誤,當你編譯時它沒有main,但沒有main不是編譯時錯誤。想想我們沒有任何想法,你會得到什麼樣的錯誤,並在問題*中提供必要的信息。 – arcy

+0

對於您的基本理解@Richard如果此代碼位於名爲Polygon.java的文件中,您的代碼將被編譯。我這樣做了,它爲我編譯沒有錯誤。當您嘗試運行此代碼時,您可能會遇到錯誤,因爲它沒有主要方法。 如果您不知道如何添加主要方法,或者您確實看到一些錯誤,請發佈您看到的所有錯誤。如果難以發佈截圖。 – Pat

回答

0

創建單獨的類在同一個包中被稱爲Main。在那裏添加主要方法。然後實例化一個對象。

public class Main { 

    public static void main(String[] args) { 

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

你的代碼編譯得很好。你所缺乏的是另一個使用你剛創建的對象類的類。

所以基本上你需要做的就是去你的編輯器(日食,記事本++等),再拍文件,並使用類似這樣的內容:

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 


    } 

}