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));
}
我的問題是,無論輸入如何,我的輸出結果都是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);
}
我需要在這裏做一些不同的事嗎?
啊,我知道這很簡單。編輯 - 嗯,我只是改變了這一點,我仍然有同樣的錯誤。 – eleven357
@ eleven357:那麼你可能仍然在進行int分割,但是在其他地方。爲什麼不嘗試做一些調試來找出***發生錯誤的位置。一個調試器或失敗,println語句應該有所幫助。 –
好吧,我發現我的問題 – eleven357