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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7911065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:58:34  来源:igfitidea点击:

my geometry program (Calculating area and volume of spheres, cylinders, and cones)

java

提问by eleven357

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));
}

My problem is that my output results in 0.0 everytime, regardless of the input. I'm sure it is probably something simple, but I can't seem to figure it out. Can you please help me out? Thanks a lot :) I appreciate your time.

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

EDIT

编辑

So I am still getting 0.0 for my output even though I changed my code like this:

因此,即使我像这样更改了代码,我的输出仍然为 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));
}

When I am calling my functions in main(), looks like this:

当我在 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);
}

Do I need to do something different here?

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

回答by Mysticial

You're dividing integers:

您正在除以整数:

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

Integer division rounds down in Java. Therefore, 1 / 3is evaluating to 0.

Java 中的整数除法四舍五入。因此,1 / 3评估为 0。

Change it to:

将其更改为:

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

Here's the other occurrence:

这是另一种情况:

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

change to:

改成:

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

EDIT :

编辑 :

You're also calling all your functions with 0.0. So of course the results will be zero.

您还使用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));

EDIT : Another Suggestion

编辑:另一个建议

You probably don't need all those Math.pow()calls since they're all called with 2 or 3. I'd say it's probably more concise and readable (and faster?) to just do:

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

radius * radius + height * height

instead of

代替

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

EDIT 2: More fixes:

编辑 2:更多修复:

Change your mainto this. And get rid of the inputfunction:

改变你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);
}

回答by user973572

I am not a Java guy but shouldn't you be storing the input into some sort of global variables? Then using those variables as the arguments to your function calls instead of 0.0? For instance, this code:

我不是 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));
}

will just print out the results of those function calls with 0.0 as the inputs, not radius/height user input.

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

And, yes, the integer math isn't helping.

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

回答by Pops

Are you typing your input by hand and relying on your Scanner? If so, that's your problem. The values from the Scannerare being assigned to your parameters in input(), and not actually being saved anywhere.

您是否手动输入输入并依赖于您的输入Scanner?如果是这样,那是你的问题。中的值Scanner被分配给您的参数input(),实际上并未保存在任何地方。

Your other methods are all being called with inputs of zero, for which outputs of 0.0would actually be correct (although that doesn't mean you're bug-free).

您的其他方法都是在输入为零的情况下调用的,对于这些方法的输出0.0实际上是正确的(尽管这并不意味着您没有错误)。

回答by mohammad

Create a class Cylinder; the class has attributes radius, and height. It has methods that calculate the surface area of a cylinder called surfaceArea and a method to calculate the volume in addition to the needed set and get methods

创建一个类Cylinder;该类具有属性半径和高度。除了所需的 set 和 get 方法外,它还有计算圆柱体表面积的方法,称为 surfaceArea 和计算体积的方法

Note : use the following formulas for calculations : volume = (pi)r^2 * h where h is the height. surface area of a cylinder=2 * pi * radius * radius + 2* pi * radius * height Cylinder - height : float - radius : float <>Cylinder(r:float,h :float) + volume() : float + surfaceArea( ) : float + setRad ( r : float) + getRad( ) : float + setHeight(h : float) + getHeight() : float

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

回答by user5000

when you using double, it should be sc.nextDouble().

当你使用 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();
}