我的几何程序(计算球体,圆柱体和圆锥体的面积和体积)

时间:2011-10-27 02:20:38

标签: java

public class Geometry {

public static void main(String[] args) {
    input(0.0, 0.0);
    sphereVolume(0.0, 0.0);
    sphereSurface(0.0, 0.0);
    cylinderVolume(0.0, 0.0);
    cylinderSurface(0.0, 0.0);
    coneVolume(0.0, 0.0);
    coneSurface(0.0, 0.0);
    output(0.0, 0.0);
}
/**
 * @param radius
 * @param height
 */
public static void input(double radius, double height) {

    Scanner sc = new Scanner(System.in);

    System.out.print("Enter radius r: ");
    radius = sc.nextInt();

    System.out.print("Enter height h: ");
    height = sc.nextInt();
}

public static double sphereVolume(double radius, double height) {
    double volume = (4 / 3) * Math.PI * Math.pow(radius, 3.0);
    return volume;
}

public static double sphereSurface(double radius, double height) {
    double surface = 4 * Math.PI * Math.pow(radius, 2.0);
    return surface;
}

public static double cylinderVolume(double radius, double height) {
    double volume = Math.PI * Math.pow(radius, 2.0) * height;
    return volume;
}

public static double cylinderSurface(double radius, double height) {
    double surface = 2 * Math.PI * radius * height + 2 * Math.PI * Math.pow(radius, 2.0);
    return surface;
}

public static double coneVolume(double radius, double height) {
    double volume = (1 / 3) * Math.PI * Math.pow(radius, 2.0) * height;
    return volume;
}

public static double coneSurface(double radius, double height) {
    double surface = Math.PI * radius * (radius + Math.pow(( Math.pow(radius, 2.0) + Math.pow(height, 2.0)), .5));
    return surface;
}

public static void output(double radius, double height) {
    System.out.printf("Volume of sphere: %f\n", sphereVolume(0.0, 0.0));
    System.out.printf("Surface area of Sphere: %f\n", sphereSurface(0.0, 0.0));
    System.out.printf("Volume of cylinder: %f\n", cylinderVolume(0.0, 0.0));
    System.out.printf("Surface area of cylinder: %f\n", cylinderSurface(0.0, 0.0));
    System.out.printf("Volume of cone: %f\n", coneVolume(0.0, 0.0));
    System.out.printf("Surface area of cone: %f\n", coneSurface(0.0, 0.0));
}

我的问题是我的输出每次都会产生0.0,无论输入如何。我敢肯定它可能很简单,但我似乎无法弄明白。你能帮帮我吗?非常感谢:)我感谢你的时间。

修改

所以即使我改变了我的代码,我的输出仍然是0.0:

    public static void output(double radius, double height) {
    System.out.printf("Volume of sphere: %.13f\n", sphereVolume(radius, height));
    System.out.printf("Surface area of Sphere: %.13f\n", sphereSurface(radius, height));
    System.out.printf("Volume of cylinder: %.13f\n", cylinderVolume(radius, height));
    System.out.printf("Surface area of cylinder: %.13f\n", cylinderSurface(radius, height));
    System.out.printf("Volume of cone: %.13f\n", coneVolume(radius, height));
    System.out.printf("Surface area of cone: %.13f\n", coneSurface(radius, height));
}

当我在main()中调用我的函数时,看起来像这样:

public static void main(String[] args) {
    input(0.0, 0.0);
    sphereVolume(0.0, 0.0);
    sphereSurface(0.0, 0.0);
    cylinderVolume(0.0, 0.0);
    cylinderSurface(0.0, 0.0);
    coneVolume(0.0, 0.0);
    coneSurface(0.0, 0.0);
    output(0.0, 0.0);
}

我需要在这里做些不同的事吗?

5 个答案:

答案 0 :(得分:4)

你要划分整数:

public static double coneVolume(double radius, double height) {
    double volume = (1 / 3) * Math.PI * Math.pow(radius, 2.0) * height;
    return volume;
}

整数分部在Java中向下舍入。因此,1 / 3正在评估为0。

将其更改为:

double volume = (1. / 3.) * Math.PI * Math.pow(radius, 2.0) * height;

这是另一种情况:

double volume = (4 / 3) * Math.PI * Math.pow(radius, 3.0);

更改为:

double volume = (4. / 3.) * Math.PI * Math.pow(radius, 3.0);

编辑:

您还使用0.0调用所有功能。所以结果当然是零。

System.out.printf("Volume of sphere: %f\n", sphereVolume(0.0, 0.0));
System.out.printf("Surface area of Sphere: %f\n", sphereSurface(0.0, 0.0));
System.out.printf("Volume of cylinder: %f\n", cylinderVolume(0.0, 0.0));
System.out.printf("Surface area of cylinder: %f\n", cylinderSurface(0.0, 0.0));
System.out.printf("Volume of cone: %f\n", coneVolume(0.0, 0.0));
System.out.printf("Surface area of cone: %f\n", coneSurface(0.0, 0.0));

编辑:另一个建议

你可能不需要所有那些Math.pow()调用,因为它们都是用2或3调用的。我会说它可能更简洁,更易读(也更快?)来做:

radius * radius + height * height

而不是

Math.pow(radius, 2.0) + Math.pow(height, 2.0)

编辑2:更多修正:

将您的main更改为此。并摆脱input函数:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.print("Enter radius r: ");
    double radius = sc.nextInt();

    System.out.print("Enter height h: ");
    double height = sc.nextInt();

    output(radius, height);
}

答案 1 :(得分:0)

我不是Java人,但你不应该将输入存储到某种全局变量中吗?然后使用这些变量作为函数调用的参数而不是0.0?例如,这段代码:

public static void output(double radius, double height) {
    System.out.printf("Volume of sphere: %f\n", sphereVolume(0.0, 0.0));
    System.out.printf("Surface area of Sphere: %f\n", sphereSurface(0.0, 0.0));
    System.out.printf("Volume of cylinder: %f\n", cylinderVolume(0.0, 0.0));
    System.out.printf("Surface area of cylinder: %f\n", cylinderSurface(0.0, 0.0));
    System.out.printf("Volume of cone: %f\n", coneVolume(0.0, 0.0));
    System.out.printf("Surface area of cone: %f\n", coneSurface(0.0, 0.0));
}

将打印出那些函数调用的结果,其中0.0为输入,而不是半径/高度用户输入。

而且,是的,整数数学没有帮助。

答案 2 :(得分:0)

您是手动输入输入并依赖Scanner吗?如果是这样,那就是你的问题。 Scanner中的值将分配给input()中的参数,而不是实际保存在任何位置。

你的其他方法都被调用,输入为零,0.0的输出实际上是正确的(虽然这并不意味着你没有错误)。

答案 3 :(得分:0)

创建一个类Cylinder;该类具有属性radius和height。它有一些方法可以计算一个名为surfaceArea的圆柱体的表面积,另一种是计算体积以及所需方法和方法的方法

注意:使用以下公式进行计算:     体积=(pi)r ^ 2 * h其中h是高度。     圆柱体的表面积= 2 * pi *半径*半径+ 2 * pi *半径*高度 圆筒 - 身高:漂浮 - radius:float <> Cylinder(r:float,h:float) + volume():float + surfaceArea():float + setRad(r:float) + getRad():float + setHeight(h:float) + getHeight():float

答案 4 :(得分:0)

当你使用double时,它应该是sc.nextDouble()。

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter radius r: ");
double radius = sc.nextDouble();

System.out.print("Enter height h: ");
double height = sc.nextDouble();
}
相关问题