Java can';在我的程序中找不到错误

Java can';在我的程序中找不到错误,java,comparable,Java,Comparable,第一个代码块以静态方法出现在我的主程序中。 在这个主程序中,我设置了多边形,它们的面积也可以在一个getArea()方法中找到,稍后我会打印出来,但在打印数据之后,我会使用这个setArea()方法来测试compareTo()方法。 第二个块是我的多边形类中覆盖的compareTo,实现comparable。 为什么setArea()方法不更新区域?我认为这可能是一个静态问题,但不确定 public static void printPolyData(){ try{

第一个代码块以静态方法出现在我的主程序中。
在这个主程序中,我设置了多边形,它们的面积也可以在一个getArea()方法中找到,稍后我会打印出来,但在打印数据之后,我会使用这个setArea()方法来测试compareTo()方法。
第二个块是我的多边形类中覆盖的compareTo,实现comparable。
为什么setArea()方法不更新区域?
我认为这可能是一个静态问题,但不确定

public static void printPolyData(){

    try{


        for(int i=0;i<=poly_count-1;i++){

        System.out.println("polygon #" + (i+1)); 
        System.out.println(poly_list.get(i).getSides() + " sides");
        System.out.println(poly_list.get(i).arrayToString());
        System.out.println("Area of Polygon = " + poly_list.get(i).getArea());
        System.out.println("Area of Polygon = " + poly_list.get(i).dListToString());


        }




                poly_list.get(0).setArea(0.00090);
                poly_list.get(1).setArea(0.00094);
                poly_list.get(2).setArea(0.000901);


                System.out.println("Comparing polygon0 (Area=" 
                + poly_list.get(0).getArea() + ") with polygon1 (Area=" 
                + poly_list.get(1).getArea() + ") : " +
                + poly_list.get(0).compareTo(poly_list.get(1)));

                System.out.println("Comparing polygon1 (Area=" 
                + poly_list.get(1).getArea() + ") with polygon0 (Area=" 
                + poly_list.get(0).getArea() + ") : " +
                + poly_list.get(1).compareTo(poly_list.get(0)));

                System.out.println("Comparing polygon0 (Area=" 
                + poly_list.get(0).getArea() + ") with polygon2 (Area=" 
                + poly_list.get(2).getArea() + ") : " +
                + poly_list.get(0).compareTo(poly_list.get(2)));


    }
    catch (Exception e){

        System.out.println("error printing polygon data. " + e.getMessage());

    }


}




 @Override
    public int compareTo(Polygon otherPoly) {



        double otherPolysArea = otherPoly.getArea(); 
        final double error = 0.00005;

        // otherPoly is bigger. current polygon smaller.
        if((this.getArea() - otherPolysArea) < -error){

            return -1;
        }
        // otherPoly is smaller. current polygon bigger.
        else if((this.getArea() - otherPolysArea) > error){

            return 1;
        }
        else{return 0;} // polygons are considered same.

    }

public void setArea(double area){


    this.area = area;

}
publicstaticvoidprintpolydata(){
试一试{
for(int i=0;i错误){
返回1;
}
否则{return 0;}//多边形被视为相同。
}
公共区域(双区域){
这个面积=面积;
}
比较polygon0(面积=2.0)和polygon1(面积=1.0):1
比较polygon1(面积=1.0)和polygon0(面积=2.0):-1
比较Polygon0(面积=2.0)和polygon2(面积=1.5):1


问题是什么?仅供参考,当您传入Polygon类型的参数时,您不必测试instanceOfdouble OtherPolyArea=((Polygon)otherPoly).getArea();//不必要的强制转换。显示setArea的代码。.您的比较看起来不错。但是对于我的2条注释,我添加了setArea()方法。我也为不同类的代码不在单独的块中表示歉意。我试图按照他们告诉我的方式格式化它,但它不配合。