gpt4 book ai didi

java - 语法错误,在java中插入 "EnumBody"和 "enum Identifier"

转载 作者:行者123 更新时间:2023-12-02 07:35:24 29 4
gpt4 key购买 nike

如果有人可以帮忙,我无法弄清楚我的代码出了什么问题,为什么它告诉我这个?它给我一个 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)

最佳答案

查看您的 getPerimeter 方法:

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



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

您正尝试在 getPerimeter 方法声明 toString 方法。

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

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

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

关于java - 语法错误,在java中插入 "EnumBody"和 "enum Identifier",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12313555/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com