2014-07-08 84 views
-2

,所以我有一個數學方程式,我需要在Java中使用,但由於某種原因,我的代碼是給我的小錯誤:(基本的數學方程式的數學到Java代碼

數學方程是在部分說明上this web page額外的信貸

我當前的代碼outpouts 4000和的answere是4005我是什麼錯史稿?

我的測試類lookes這樣

 public class MainActivity { 
      public static void main(String[] args) throws Exception{ 
       double baseMaterial =556; 
       int me =5; 
       int ml = 10; 
       int extraMaterial = 3444; 
       System.out.println(""+calculateMiniralTotal(baseMaterial,me,ml,extraMaterial)); 
      } 
      public static double calculateMiniralTotal(double perfekt,int me,int ml,int extraMaterial) { 
       double s = (perfekt + (perfekt * (10/(ml + 1))/100)); 
       s = Math.round(s); 
       double r = s + (perfekt * (0.25 - (0.05 * me))); 
       r = Math.round(r); 
       double q = extraMaterial + (extraMaterial * (0.25 - (0.05 * me))); 
       q = Math.round(q); 
       //double r=q; 
       r = r + q; 
       return Math.round(r); 
      } 
     } 
+0

您的變量名稱很難理解,它使代碼無法讀取。 – BitNinja

+0

本月晚些時候,行業變化將首先在下一個發佈活動中變得活躍。 和我正在使用fuzzworks材料計算器來檢查錯誤巫婆在蒂斯方程上運行:P – fantazituborg

回答

1

您正在執行整數除法(10/(ml + 1))/100,這在Java中必須導致另一個int。您的ml10,在Java中,10/110,而不是0.909 ...,並且沒有任何內容添加到s

使用double文字或強制轉換爲double來強制執行浮點計算。

double s = (perfekt + (perfekt * (10.0/(ml + 1))/100)); 

double s = (perfekt + (perfekt * ((double) 10/(ml + 1))/100)); 

製作要麼改變使得輸出:

4005.0 
+0

omg我花了這麼多小時試圖追查這個問題:(和答案是這麼簡單:( 坦克很多! – fantazituborg

0

當你乘雙層由一個int你會得到一個int回來。

public class Main 
{ 

    public static void main(String[] args) 
     throws Exception 
    { 
     double baseMaterial = 556; 
     int me = 5; 
     int ml = 10; 
     int extraMaterial = 3444; 
     System.out.println("" + calculateMiniralTotal(baseMaterial, me, ml, extraMaterial)); 

    } 

    public static double calculateMiniralTotal(double perfekt, int me, int ml, int extraMaterial) 
    { 
     double s = (perfekt + (perfekt * (10.0/(ml + 1))/100.0)); // <-- changed from 10 to 10.0 and 100 to 100.0. This way they are doubles too 
     s = Math.round(s); 
     double r = s + (perfekt * (0.25 - (0.05 * me))); 
     r = Math.round(r); 
     double q = extraMaterial + (extraMaterial * (0.25 - (0.05 * me))); 
     q = Math.round(q); 
     //       double r=q; 

     r = r + q; 
     return Math.round(r); 
    } 
}