2014-09-12 61 views
-1

我正在編寫一個非常簡單的程序,用戶試圖找出他是否改變了煎餅的直徑(新舊直徑都是用戶輸入的),給定數量(用戶輸入),可以製作多少個具有新直徑的薄煎餅。現在我的計算與測試數據一起工作:10箇舊直徑,8箇舊數量,4個新直徑。這導致了50個新的煎餅是正確的,但是當我輸入5箇舊的直徑,10箇舊的數量,10個新的直徑時。這導致1.59999新煎餅不正確,答案應該是2.5。這裏是我的代碼,任何幫助,將不勝感激:Java程序對某些數據集計算不正確,但對其他數據集正確計算

import java.util.Scanner; 
public class pancakes { 

    public static void main(String[] args) { 
     int diameterOld = 0;             // used to save diameter of original pancakes 
     int quantityOld = 0;             // used to save quantity of original pancakes 
     int diameterNew = 0;             // used to save diameter of new pancakes 
     double quantityNew = 0;             // used to save quantity of new pancakes 
     double areaNew = 0;              // used to store area of new pancakes 
     double areaOld = 0;              // used to store area of original pancakes 
     double totalSurfaceArea = 0;           // total amount of batter used 

     Scanner input = new Scanner(System.in);         // allows keyboard input 

     System.out.println ("Mohammad's java Pancakes");       
     System.out.print ("Diameter of original pancakes (inches): ");   // Prints statement asking for diameter of original pancakes 
     diameterOld = input.nextInt();           // saves input into diameterOld 
     System.out.print ("Quantity of original pancakes: ");     // Prints statement asking for quantity of original pancakes 
     quantityOld = input.nextInt();           // saves input into quantityOld 
     System.out.print ("Diameter of new pancakes (inches): ");    // Prints statement asking for diameter of new pancakes 
     diameterNew = input.nextInt();           // saves input into diameterNew 

     areaOld = ((diameterOld/2)*(diameterOld/2)*Math.PI);     // calculates area of original pancakes 
     totalSurfaceArea = (areaOld * quantityOld);        // calculates total amount of batter needed 
     areaNew = ((diameterNew/2)*(diameterNew/2)*Math.PI);     // calculates area of new pancakes 
     quantityNew = (totalSurfaceArea/areaNew);        // calculates quantity of new pancakes 

     System.out.println ("Quantity of new pancakes: " + quantityNew);  // Prints statement stating quantity of new pancakes and amount of new pancakes 

     input.close();               // Closes scanner input 
    } 

} 
+0

困惑......不會老5直徑,10歲數量,5個新的直徑= 0新的煎餅? – 2014-09-12 19:49:20

+0

我只得到1.59999999,如果我輸入5,10,10。 – rgettman 2014-09-12 19:50:52

回答

1

當你把(diameterOld/2),同樣表情,你將兩個整數,因此結果將是一個整數。因此,如果diameterOld等於5,則(diameterOld/2)不會像您期望的那樣給出2.5,而是它會截斷爲整數,因此您會得到2

要糾正此,投整數值到雙之一執行除法,像以前一樣:其中在(diameterOld/2.0)

+0

謝謝男人,我覺得很愚蠢的想念這麼簡單的哈哈。非常感謝 – Horrerblade 2014-09-12 20:00:32

+0

不客氣。不要忘記通過選擇一個答案來關閉問題 – 2014-09-12 20:04:15

1

您與4次diameterOld(兩次)和diameterNew執行整數除法(兩次), Java導致另一個整數。即使數學結果爲2.5,Java也會將其截斷爲2int)。

變化由double文字分裂,迫使浮點運算從一開始,例如:

diameterOld/2.0 

,同樣也像一個其他部門。

另外,您將totalSurfaceArea除以areaNew,兩者均將直徑除以2並乘以pi。在數學上,這些部分取消了,所以你甚至不需要打擾2的分割或乘以pi。

areaOld = diameterOld * diameterOld; 

areaNew = diameterNew * diameterNew; 

此計算quantityNew當數學上成立。

輸出:

Mohammad's java Pancakes 
Diameter of original pancakes (inches): 5 
Quantity of original pancakes: 10 
Diameter of new pancakes (inches): 10 
Quantity of new pancakes: 2.5 
+0

感謝您的詳細解釋,非常感謝。我在想,pi和(/ 2)的會取消,但我反正把它編碼了。 – Horrerblade 2014-09-12 20:24:47

0

的區域舊您的代碼應該是

areaOld = (((double)(diameterOld/2))*((double)(diameterOld/2))*Math.PI); 
areaNew = (((double)(diameterNew/2))*((double)(diameterNew/2))*Math.PI); 
+0

我不這麼認爲。你必須在分割之前進行類型轉換。 – 2014-09-12 19:54:09

+0

@TylerGaona問題是他在做一個int int將會隱式返回n int ...所以我們需要將它轉換爲double。 – StackFlowed 2014-09-12 20:01:06

+0

在事實之後鑄造成雙層將不會給出預期的結果。它需要在師之前完成 – 2014-09-12 20:03:13