球体积法不起作用
Sphere volume method not working
我已经尝试了很多不同的计算来尝试让我的球体体积法起作用。
我的 Sphere class 从 Circle 扩展而来,从 Circle 中获取面积,并实现了我的 Shape3D 接口,它允许我使用我的体积方法。
但是,我已经为我的方法尝试了所有这些不同的公式,但没有任何东西能给我返回准确的球体体积。它总是彻底关闭。
最接近的是(4*22*radius * radius * radius )/(3*7);但它仍然关闭。
//return (4/3) * super.area() * radius;
//return (Double)((4*22*getRadius()*getRadius()*getRadius() )/(3*7));
//return (4*22*radius * radius * radius )/(3*7);
//return ( (4/3) * (Math.pow(getRadius(), 3) ) / (Math.PI) );
//return (getRadius() * getRadius() * getRadius() ) * (Math.PI) * (4/3);
//return ( super.area() * getRadius() ) * (4/3);
我将附上我的 Shape 抽象 class、Circle class 和 Sphere class 以及 Shape3D 界面的代码。
也许我忽略了一些显而易见的事情。但是,当我设置半径并将其设置为正常时,半径 returns 恢复正常。所以我不确定为什么每一个都完全关闭。
public class Main {
public static void main(String[] args) {
System.out.println(volume());
}
public static double volume() {
double vol;
double x = 4/3;
double y = Math.pow(30.0, 3);
double z = Math.PI;
vol = y * z * x;
return vol;
//return (4/3) * super.area() * radius;
//return (Double)((4*22*getRadius()*getRadius()*getRadius() )/(3*7));
//return (4*22*radius * radius * radius )/(3*7);
//return ( (4/3) * (Math.pow(getRadius(), 3) ) / (Math.PI) );
//return (getRadius() * getRadius() * getRadius() ) * (Math.PI) * (4/3);
//return ( super.area() * getRadius() ) * (4/3);
}// end sphere volume
}
这是一个简单的 java 测试程序,用于计算球体的体积,您可以将其用作您做错了什么的参考,即 4/3
returns int, 1 rather than a double representation of ,因此打乱了你的计算:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// notice the descriptive variable names rather than x, y and z
double radius;
double diameter;
double volume;
System.out.print("Enter the radius of the sphere:");
radius = scanner.nextDouble();
diameter = (radius * 2.0);
volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3); // instead of 4/3 in yours
System.out.println("The volume is: " + String.format("%.1f", volume));
}
}
用法示例:
Enter the radius of the sphere: 5
The volume is: 523.6
要确认它是否正常工作,您可以使用 Google's sphere volume calculator 并确认它给出相同的值:
而不是
double x = 4/3;
尝试
double x = 4.0/3.0;
或
double x = 4.0/3;
或
double x = 4/3.0;
我已经尝试了很多不同的计算来尝试让我的球体体积法起作用。
我的 Sphere class 从 Circle 扩展而来,从 Circle 中获取面积,并实现了我的 Shape3D 接口,它允许我使用我的体积方法。
但是,我已经为我的方法尝试了所有这些不同的公式,但没有任何东西能给我返回准确的球体体积。它总是彻底关闭。
最接近的是(4*22*radius * radius * radius )/(3*7);但它仍然关闭。
//return (4/3) * super.area() * radius;
//return (Double)((4*22*getRadius()*getRadius()*getRadius() )/(3*7));
//return (4*22*radius * radius * radius )/(3*7);
//return ( (4/3) * (Math.pow(getRadius(), 3) ) / (Math.PI) );
//return (getRadius() * getRadius() * getRadius() ) * (Math.PI) * (4/3);
//return ( super.area() * getRadius() ) * (4/3);
我将附上我的 Shape 抽象 class、Circle class 和 Sphere class 以及 Shape3D 界面的代码。
也许我忽略了一些显而易见的事情。但是,当我设置半径并将其设置为正常时,半径 returns 恢复正常。所以我不确定为什么每一个都完全关闭。
public class Main {
public static void main(String[] args) {
System.out.println(volume());
}
public static double volume() {
double vol;
double x = 4/3;
double y = Math.pow(30.0, 3);
double z = Math.PI;
vol = y * z * x;
return vol;
//return (4/3) * super.area() * radius;
//return (Double)((4*22*getRadius()*getRadius()*getRadius() )/(3*7));
//return (4*22*radius * radius * radius )/(3*7);
//return ( (4/3) * (Math.pow(getRadius(), 3) ) / (Math.PI) );
//return (getRadius() * getRadius() * getRadius() ) * (Math.PI) * (4/3);
//return ( super.area() * getRadius() ) * (4/3);
}// end sphere volume
}
这是一个简单的 java 测试程序,用于计算球体的体积,您可以将其用作您做错了什么的参考,即 4/3
returns int, 1 rather than a double representation of
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// notice the descriptive variable names rather than x, y and z
double radius;
double diameter;
double volume;
System.out.print("Enter the radius of the sphere:");
radius = scanner.nextDouble();
diameter = (radius * 2.0);
volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3); // instead of 4/3 in yours
System.out.println("The volume is: " + String.format("%.1f", volume));
}
}
用法示例:
Enter the radius of the sphere: 5
The volume is: 523.6
要确认它是否正常工作,您可以使用 Google's sphere volume calculator 并确认它给出相同的值:
而不是
double x = 4/3;
尝试
double x = 4.0/3.0;
或
double x = 4.0/3;
或
double x = 4/3.0;