我有這段代碼,我缺少靜態字段。我如何在這個程序中做靜態字段?我是否也需要實例字段?如何做靜態方法?
import java.util.Scanner;
public class Geometry
{
public static double sphereVolume(double r)
{
double svol=(4.0/3.0)*Math.PI*(Math.pow(r,3));
return svol;
}
public static double sphereSurface(double r)
{
double ssur=4.0*Math.PI*(Math.pow(r,2));
return ssur;
}
public static double cylinderVolume(double r, double h)
{
return (Math.PI*r*r*h);
}
public static double cylinderSurface(double r, double h)
{
double csur=2*(Math.PI*(Math.pow(r,2)))+(2*Math.PI*r)*h;
return csur;
}
public static double coneVolume(double r, double h)
{
return (Math.PI/3.0)*h*(Math.pow(r,2));
}
public static double coneSurface(double r, double h)
{
return Math.PI*r*r+Math.PI*r*Math.sqrt(Math.pow(r,2)+(Math.pow(h,2)));
}
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("What is the radius?");
String input=in.nextLine();
double r=Double.parseDouble(input);
System.out.println("The volume of the sphere:"+sphereVolume(r));
System.out.println("The Surface Area of the sphere:"+sphereSurface(r));
System.out.println("The Surface Area of the sphere:"+cylinderSurface(r,h));
System.out.println("The volume of the cone:"+coneVolume(r,h));
System.out.println("The Surface Area of the cone:"+coneSurface(r,h));
}
}
靜態變量可以定義爲static double variablename; –