對於此問題,我需要編寫一個程序,提示並詢問用戶以英寸爲單位的球體寬度,然後計算以英寸爲單位的面積,以英尺爲單位的面積,體積以英寸爲單位,以英尺爲單位的體積和加侖水.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英寸的球體的加侖容量...... –