对于此问题,我需要编写一个程序,提示并询问用户以英寸为单位的球体宽度,然后计算以英寸为单位的面积,以英尺为单位的面积,体积以英寸为单位,以英尺为单位的体积和加仑水.Pi和加仑转换变量(7.48)是常数。用于得到这些计算的公式如下:在水塔计算器程序中计算球体积 - Java
A = 4*pi*r^2
AFt = A/12
V = 4/3 * pi * r^3
VFt = V/1728
Gallons of water = VFt * 7.48
The test width value is 400. This should give me the output of
A = 502,400.00
AFt = 41,866.67
V = 33,493,333.33
VFt = 2,791,111.11
Gallons of Water = 20,877,511.11
这是我的代码到目前为止。前两个计算是正确的,但我无法正确计算体积,以英尺为单位的体积和加仑水。任何帮助深表感谢。
import java.util.Scanner;
public class WaterTowerCalculator {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Enter the width of the sphere");
int width = scan.nextInt();
final double PI = 3.14;
final double C_VARIABLE = 7.48;
double w = width;
double r = w/2;
double A = 4*PI*r*r;
double AFt = (double) A/12;
double V = (4/3)*PI*(r*r*r);
double VFt = (double) V/1728;
double gallons = (double) VFt * C_VARIABLE;
System.out.printf("Area in inches " +"%,.2f\n", A);
System.out.printf("Area in feet " + "%,.2f\n", AFt);
System.out.printf("Volume in inches " + "%,.2f\n", V);
System.out.printf("Volume in feet " + "%,.2f\n", VFt);
System.out.printf("Gallons of water " + "%,.2f\n", gallons);
}
}
说*我遇到了麻烦*作为问题描述完全没用,除非你明确解释你遇到的麻烦*。我们可以帮助您解决哪些**具体问题? –
你应该使'width'应该是'double'并且用'nextDouble()'接受它。否则,'r'的计算是用整数算术完成的,如果'width'是奇数,将不会给出正确的结果。除此之外,球体并不总是以很好的方便整数英寸宽;为什么呢,就在那一天我需要找到一个直径正好是214.4358349英寸的球体的加仑容量...... –