语法错误,插入" EnumBody"和" enum标识符"在java中

时间:2012-09-07 07:21:09

标签: java enums syntax-error tostring

如果有人可以提供帮助,我无法弄清楚我的代码有什么问题,为什么告诉我这个?它给了我一个toString方法的错误...为什么它让我插入枚举体和标识符?谢谢

  import java.lang.String;

  public class Circle extends Shape {
  private double radius;

  public Circle( double theRadius ){
    super();
    if ( theRadius <= 0.0 )
        setRadius( Shape.DEFAULT_SIZE );
    else
        setRadius( theRadius );
}



public double getSurfaceArea(){

    return this.radius * this.radius * Math.PI;
}

public double getPeremeter(){
    ;
    return 2 * this.radius + Math.PI;
}

public double getRadius(){
    return this.radius;

}

public void setRadius( double theRadius ) {
    if( theRadius <= 0 )
        return;
    this.radius = theRadius;
}

@Override
public double getPerimeter() {
    // TODO Auto-generated method stub
    return 0;



     public String toString() {
         return "Circle Surface Area "+getSurfaceArea()+", Circle Peremeter "       +getPerimeter();
     }
}

@Override
public double getSizeAmount() {
    // TODO Auto-generated method stub
    return 0;
}
   }

主要班级

import javax.swing.JOptionPane;
 import java.text.DecimalFormat; 



 public class ShapeApp {


public static <RectangularPrism> void main(String[] args) {



    int x = 0;
    Triangle triangleObjects[] = new Triangle[ 3 ];

    triangleObjects[ 0 ] = new Rectangle("3.5","4.6");
    triangleObjects[ 1 ] = new Rectangle("3","2");

    triangleObjects[ 2 ] = new Circle(0);

    System.out.println( "List of all Shapes:\n" );
    do{
    try{
        for( Triangle currentTriangle : triangleObjects ) {
        String msg = currentTriangle.getSurfaceArea() +","+ currentTriangle.getPerimeter();
        JOptionPane.showMessageDialog(null, "Message", msg, JOptionPane.INFORMATION_MESSAGE);
        x=2;
    }

    }catch(Exception e) {

    }

}while(x==1);


    }


}

错误

  Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error on token "String", @ expected
Syntax error, insert "enum Identifier" to complete EnumHeaderName
Syntax error, insert "EnumBody" to complete BlockStatement

at Circle.getPerimeter(Circle.java:44)
at ShapeApp.main(ShapeApp.java:80)

2 个答案:

答案 0 :(得分:2)

查看您的getPerimeter方法:

@Override
public double getPerimeter() {
    // TODO Auto-generated method stub
    return 0;



     public String toString() {
         return "Circle Surface Area "+getSurfaceArea()+", Circle Peremeter "
              +getPerimeter();
     }
}

您尝试在 toString方法中声明getPerimeter方法

代码的缩进应该给你一个暗示 - 这是为什么适当地缩进代码很重要的一个原因。你应该:

@Override
public double getPerimeter() {
    // TODO Auto-generated method stub
    return 0;
}    

@Override
public String toString() {
    return "Circle Surface Area " + getSurfaceArea() + ", Circle Perimeter "       
        + getPerimeter();
}

答案 1 :(得分:0)

@Override
public double getPerimeter() {
    // TODO Auto-generated method stub
    return 0;



     public String toString() {
         return "Circle Surface Area "+getSurfaceArea()+", Circle Peremeter "       +getPerimeter();
     }
}

应分为两部分:

@Override
public double getPerimeter() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public String toString() {
             return "Circle Surface Area "+getSurfaceArea()+", Circle Peremeter "       +getPerimeter();
         }