2011-10-27 71 views
1
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); 
} 

我需要在這裏做一些不同的事嗎?

回答

4

你除以整數:

public static double coneVolume(double radius, double height) { 
    double volume = (1/3) * Math.PI * Math.pow(radius, 2.0) * height; 
    return volume; 
} 

的整數除法的Java幾輪下來。因此,1/3被評估爲0

將其更改爲:

double volume = (1./3.) * Math.PI * Math.pow(radius, 2.0) * height; 

這裏的其他發生:

double volume = (4/3) * Math.PI * Math.pow(radius, 3.0); 

變化:

double volume = (4./3.) * Math.PI * Math.pow(radius, 3.0); 

編輯:

您也在用0.0致電您的所有功能。所以當然結果將是零。

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

編輯:另一個建議

你可能並不需要所有這些Math.pow()電話,因爲他們都被稱爲有2或3。我會說這是可能更簡明易讀(和?快)只是做:

radius * radius + height * height 

,而不是

Math.pow(radius, 2.0) + Math.pow(height, 2.0) 

編輯2:更多的修正:

更改main這一點。而擺脫input功能:

public static void main(String[] args) { 

    Scanner sc = new Scanner(System.in); 

    System.out.print("Enter radius r: "); 
    double radius = sc.nextInt(); 

    System.out.print("Enter height h: "); 
    double height = sc.nextInt(); 

    output(radius, height); 
} 
+0

啊,我知道這很簡單。編輯 - 嗯,我只是改變了這一點,我仍然有同樣的錯誤。 – eleven357

+1

@ eleven357:那麼你可能仍然在進行int分割,但是在其他地方。爲什麼不嘗試做一些調試來找出***發生錯誤的位置。一個調試器或失敗,println語句應該有所幫助。 –

+0

好吧,我發現我的問題 – eleven357

0

我不是一個Java的傢伙,但你不應該被輸入存儲成某種全局變量?然後使用這些變量作爲你的函數調用的參數而不是0.0?例如,下面的代碼:

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

你是用手輸入你的輸入信號並依靠你的Scanner?如果是這樣,那就是你的問題。來自Scanner的值正在分配給您的參數input(),而不是實際保存在任何地方。

您的其他方法都被調用輸入爲零,其輸出0.0實際上是正確的(儘管這並不意味着你沒有錯誤)。

0

創建一個類Cylinder;該類具有屬性半徑和高度。它具有計算被稱爲surfaceArea的圓柱體表面積的方法和計算體積的方法以及所需的設置和獲得方法

注意:使用以下公式進行計算: volume =(pi)r^2 * h其中h是高度。氣缸= 2 * pi的 表面積*半徑*半徑+ 2 * PI *半徑*高度 氣缸 - 高度:浮 - 半徑:浮 <>氣缸(R:浮子,H:浮動) +體積():浮動 +表面積():浮動 + setRad(R:浮點) + getRad():浮動 +自動調用setHeight(H:浮點) +的getHeight():浮動時使用雙

+1

歡迎來到stackoverflow。這個問題很老,已經回答了。通常情況下,最好不要復活陳舊的線索,除非您的回答與之前的回答有所不同。 – Leigh

0

,它應該是sc.nextDouble()。

public static void main(String[] args) { 

Scanner sc = new Scanner(System.in); 

System.out.print("Enter radius r: "); 
double radius = sc.nextDouble(); 

System.out.print("Enter height h: "); 
double height = sc.nextDouble(); 
}