java 语法错误,在java中插入“EnumBody”和“enum Identifier”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12313555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:24:32  来源:igfitidea点击:

Syntax error, insert "EnumBody" and "enum Identifier" in java

javaenumssyntax-errortostring

提问by Angela

im cant figure out what is wrong with my code if someone can help , why is it telling me this ? it gives me an error in toString method... why does it give me to insert enum body and identifier ? thank you

如果有人可以提供帮助,我无法弄清楚我的代码有什么问题,为什么要告诉我这个?它在 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;
}
   }

main class

主班

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);


    }


}

error

错误

  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)

回答by Jon Skeet

Look at your getPerimetermethod:

看看你的getPerimeter方法:

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



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

You're trying to declare the toStringmethod withinthe getPerimetermethod.

你想申报toString方法getPerimeter方法。

The indentation of the code should have given you a hint about that - it's one reason why it's important to indent code appropriately. You should have:

代码的缩进应该给你一个提示——这是适当缩进代码很重要的一个原因。你应该有:

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

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

回答by Michael Laffargue

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



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

should be separated in two :

应该分为两部分:

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

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