计算水塔计算器程序中的球体积 - Java

时间:2017-09-12 22:24:02

标签: java

对于这个问题,我需要编写一个程序,提示并询问用户以英寸为单位的球体宽度,然后计算以英寸为单位的面积,以英尺为单位的面积,以英寸为单位的体积,以英尺为单位的体积以及加仑水.Pi和加仑转换变量(7.48)是常数。用于获得这些计算的公式是:

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

        }

    }

1 个答案:

答案 0 :(得分:0)

当你想要浮点除法时,看起来你正在使用整数除法。

替换例如4/34.0 / 3.0