Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的几何程序(计算球体、圆柱体和圆锥体的面积和体积)_Java - Fatal编程技术网

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

Java 我的几何程序(计算球体、圆柱体和圆锥体的面积和体积),java,Java,我的问题是,无论输入如何,每次输出结果都是0.0。我肯定这可能很简单,但我似乎不明白。你能帮帮我吗?非常感谢:谢谢你抽出时间 编辑 因此,尽管我对代码进行了如下更改,但我的输出仍为0.0: public class Geometry { public static void main(String[] args) { input(0.0, 0.0); sphereVolume(0.0, 0.0); sphereSurface(0.0, 0.0); cylinde

我的问题是,无论输入如何,每次输出结果都是0.0。我肯定这可能很简单,但我似乎不明白。你能帮帮我吗?非常感谢:谢谢你抽出时间

编辑

因此,尽管我对代码进行了如下更改,但我的输出仍为0.0:

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

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

您正在除以整数:

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);
}
整数除法在Java中向下舍入。因此,1/3的值为0

将其更改为:

public static double coneVolume(double radius, double height) {
    double volume = (1 / 3) * Math.PI * Math.pow(radius, 2.0) * height;
    return volume;
}
下面是另一个例子:

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

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

您还使用0.0调用了所有函数。所以结果当然是零

double volume = (4. / 3.) * Math.PI * Math.pow(radius, 3.0);
编辑:另一个建议

您可能不需要所有那些Math.pow调用,因为它们都是用2或3调用的。我想说它可能更简洁、可读性更强、速度更快?要做到这一点:

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

radius * radius + height * height
编辑2:更多修复:

把你的主菜换成这个。并去掉输入函数:

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

您正在除以整数:

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);
}
整数除法在Java中向下舍入。因此,1/3的值为0

将其更改为:

public static double coneVolume(double radius, double height) {
    double volume = (1 / 3) * Math.PI * Math.pow(radius, 2.0) * height;
    return volume;
}
下面是另一个例子:

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

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

您还使用0.0调用了所有函数。所以结果当然是零

double volume = (4. / 3.) * Math.PI * Math.pow(radius, 3.0);
编辑:另一个建议

您可能不需要所有那些Math.pow调用,因为它们都是用2或3调用的。我想说它可能更简洁、可读性更强、速度更快?要做到这一点:

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

radius * radius + height * height
编辑2:更多修复:

把你的主菜换成这个。并去掉输入函数:

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

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

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);
}
将只打印那些以0.0作为输入的函数调用的结果,而不是半径/高度用户输入


是的,整数数学也帮不上忙。

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

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);
}
将只打印那些以0.0作为输入的函数调用的结果,而不是半径/高度用户输入


而且,是的,整数数学也帮不上忙。

你是用手工输入,还是依靠扫描仪?如果是这样,那就是你的问题。扫描仪中的值被指定给输入中的参数,而不是实际保存在任何位置


您的其他方法都是使用零输入调用的,0.0的输出实际上是正确的,尽管这并不意味着您没有bug。

您是手动输入并依靠扫描仪吗?如果是这样,那就是你的问题。扫描仪中的值被指定给输入中的参数,而不是实际保存在任何位置


您的其他方法都使用零输入调用,0.0的输出实际上是正确的,尽管这并不意味着您没有bug。

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

注:使用以下公式进行计算: 体积=pir^2*h,其中h是高度。 圆柱体的表面积=2*pi*半径*半径+2*pi*半径*高度 圆柱 -高度:浮动 -半径:浮动 油缸:浮动,h:浮动 +音量:浮动 +浮子 +SETR:浮动 +getRad:浮动 +设置高度:浮动
+getHeight:float

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

注:使用以下公式进行计算: 体积=pir^2*h,其中h是高度。 圆柱体的表面积=2*pi*半径*半径+2*pi*半径*高度 圆柱 -高度:浮动 -半径:浮动 油缸:浮动,h:浮动 +音量:浮动 +浮子 +SETR:浮动 +getRad:浮动 +设置高度:浮动
+getHeight:float

使用double时,它应该是sc.nextDouble

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

当您使用double时,它应该是sc.nextDouble

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

啊,我知道这很简单。编辑-我刚刚更改了它,但仍然有相同的错误。@eleven357:那么您可能仍然在进行整数除法,但在其他地方。为什么不试着做一点调试来找出错误发生的地方呢。如果调试器失败,println语句应该会有所帮助。您也在使用0.0调用函数。我刚刚编辑了我的
n到我的答案。我如何用半径和高度的输入值调用函数?我刚刚在我的答案中编辑了它。研究一下,你就会明白为什么它会起作用。我还没有测试过。啊,我知道这很简单。编辑-我刚刚更改了它,但仍然有相同的错误。@eleven357:那么您可能仍然在进行整数除法,但在其他地方。为什么不试着做一点调试来找出错误发生的地方呢。如果调试器失败,println语句应该会有所帮助。您也在使用0.0调用函数。我刚刚在我的答案中编辑了它。我如何用半径和高度的输入值调用函数?我刚刚在我的答案中编辑了它。研究一下,你就会明白为什么它会起作用。不过我还没有测试过。欢迎来到stackoverflow。这个问题由来已久,已经得到了回答。通常情况下,除非您的响应与以前的答案相比有显著的新内容或不同之处,否则最好不要恢复过时的线程。欢迎使用stackoverflow。这个问题由来已久,已经得到了回答。通常情况下,最好不要恢复陈旧的线程,除非您的响应提供了一些明显新的或与以前的答案不同的内容。