以下代码中有错误吗?它显示找不到符号,符号:类出位置:类系统。在日志中,它显示了很多错误,包括
java.lang.ClassFormatError:类Area中的方法“”具有非法签名“(Ljava / lang / Object;)Ljava / lang / System $ out $ println;”
import java.util.*; class Area { double pi=3.14; Scanner sc=new Scanner(System.in); System.out.println("Enter the value of r"); int r=sc.nextInt(); System.out.println("enter the value h"); int h=sc.nextInt(); void areaOfCircle() { double area1=pi*r*r; System.out.println("area of circle="+area1); } void areaOfCylinder() { double area2=2*pi*r*(r+h); System.out.println("area of cylinder="+area2); } public static void main(String args[]) { Area a=new Area(); a.areaOfCircle(); a.areaOfCylinder(); } }
该语句System.out.println("");应写在某些块中。不能直接在课堂上写。
System.out.println("");
public class ClassName { System.out.println("this statement gives error"); // Error!! }
它应该放在大括号内,{...}例如:
{...}
{ System.out.println("this works fine"); }
这种方式是一个初始化程序块。
或应使用类似以下的方法编写:
public void methodName(){ System.out.println("inside a method, prints fine"); }
您的完整程序 应类似于:
public class Area { double pi = 3.14; int r; int h; void areaOfCircle() { double area1 = pi * r * r; System.out.println("area of circle=" + area1); } void areaOfCylinder() { double area2 = 2 * pi * r * (r + h); System.out.println("area of cylinder=" + area2); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the value of r"); Area a = new Area(); a.r = sc.nextInt(); System.out.println("enter the value h"); a.h = sc.nextInt(); a.areaOfCircle(); a.areaOfCylinder(); } }