Java:创建程序以查找圆柱体的表面积和体积

时间:2015-05-10 17:43:56

标签: java

我正在尝试使用两种方法制作程序,这两种方法可以计算并返回圆柱体的表面积和体积。

当我运行程序时,系统输出0.00两者,我不确定我做错了什么。

这是我的代码:

public class Circle {
public static int height; 
public static int radius; 
public static double pi = 3.14; 

public Circle(int height, int radius) { 
    height = 10; 
    radius = 5; 
}


public static double getSurfaceArea() {
    int radiusSquared = radius * radius; 
    double surfaceArea = 2 * pi * radius * height + 2 * pi * radiusSquared; 
    return surfaceArea; 
}

public static double getVolume() { 
    int radiusSquared = radius * radius; 
    double volume = pi * radiusSquared * height; 
    return volume; 
}


public static void main(String[] args) { 
    System.out.println("The volume of the soda can is: " + getVolume() + "."); 
    System.out.println("The surface area of the soda is: " + getSurfaceArea() + "."); 
}

}

提前致谢。

2 个答案:

答案 0 :(得分:1)

您必须将这行代码添加到主页:

Circle c = new Circle(10,5);

所以就是这样:

public static void main(String[] args) { 
    Circle c = new Circle(10,5);
    System.out.println("The volume of the soda can is: " + c.getVolume() + "."); 
    System.out.println("The surface area of the soda is: " + c.getSurfaceArea() + "."); 
}

并将您的圈子构造函数方法更改为:

public Circle(int height, int radius) {
this.height = height;
this.radius = radius;
}

答案 1 :(得分:0)

我相信这就是你要找的东西:

public class Circle {
public static int height; 
public static int radius; 
public static double pi = 3.14; 

public Circle(int height, int radius) { 
    this.height = height; 
    this.radius = radius; 
}


public static double getSurfaceArea() {
    int radiusSquared = radius * radius; 
    double surfaceArea = 2 * pi * radius * height + 2 * pi * radiusSquared; 
    return surfaceArea; 
}

public static double getVolume() { 
    int radiusSquared = radius * radius; 
    double volume = pi * radiusSquared * height; 
    return volume; 
}


public static void main(String[] args) { 
Circle circle = new Circle(10,5);
    System.out.println("The volume of the soda can is: " + circle.getVolume() + "."); 
    System.out.println("The surface area of the soda is: " + cirlce.getSurfaceArea() + "."); 
}
}