2013-12-11 49 views
0

當我嘗試使用gravity2方法時,我得到零輸出,但我仍然不完全知道分離方法如何幫助,因爲我是新手,接觸不同方法的規則。我怎樣才能打電話給方法打印q[]我的輸出結果爲零

public class Gravity 
{ 

    public static double[] gravity(double[] radius, double[] z, double[] radius1) 
    { 
     for (int index = 0; index < radius.length; index++) 
     { 

      z[index] = radius[index] * radius1[index]; 

     } 

     return z; 
    } 

    public static double[] gravity2(double[] q, double[] masses, double[] z) 
    { 
     for (int index = 0; index < masses.length; index++) 
     { 
      q[index] = (z[index] * 6.67E-11)/masses[index]; 
     } 
     return z; 

    } 

    public static void printResults(String[] names, double[] z, String[] m, double[] q) throws IOException 
    { 
     System.out.println(" Plantetary Data"); 
     System.out.println("====================================="); 
     System.out.println("Planet Diameter Mass Gravity"); 
     for (int index = 0; index < names.length; index++) 
     { 
      System.out.printf("%1s ", names[index]); 
      System.out.printf("%7.1f", z[index]); 
      System.out.printf("%10s", m[index]); 
      System.out.printf("%6.1f", q[index]); 
      System.out.printf("%n"); 
     } 
    } 

    public static void main(String[] args) throws IOException 
    { 
     // Initialize variables 
     String[] names = 
     { 
      "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" 
     }; 
     double[] radius = 
     { 
      2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195 
     }; 
     double[] masses = 
     { 
      3.3022 * Math.pow(10, 23), 4.8685 * Math.pow(10, 24), 5.9736 * Math.pow(10, 24), 6.4185 * Math.pow(10, 23), 1.8986 * Math.pow(10, 27), 5.6846 * Math.pow(10, 26), 8.6810 * Math.pow(10, 25), 1.0243 * Math.pow(10, 26), 1.312 * Math.pow(10, 22) 
     }; 
     8.68E+25 

    , 1.02E+26, 1.27E+22}; // See IMACS double lesson for big E notation 
     double[] radius1 = 
    { 
     2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195 
    }; 
    double z[] = new double[10]; 
    double q[] = new double[10]; 
    String m[] = 
    { 
     "3.30E+23", "4.87E+24", "5.97E+24", "6.42E+23", "1.90E+27", " 5.68E+26", " 8.68E+25", "1.02E+26", " 1.27E+22" 
    }; 
    // Processing 
    double gravities1[] = gravity(radius, z, radius1); 
    double gravities2[] = gravity2(q, masses, z); 
     // Output 

    printResults(names, z, m, q); 

    printToFile(gravities1); 

    printToFile(gravities2); 
} 
+0

是您的代碼中模擬兩個合成的元素之間的熱核反應? –

+0

@ Trojan.ZBOT它正在解決行星之間的重量差異 – FlyingKalamari

+0

你的代碼中缺少一些東西。在數組'質量'和'半徑1'之間數字不與任何東西連接。 – Pawel

回答

0

的問題是,您在q值是非常小的,在10E-27的順序,所以當你試圖用這麼少的小數位打印,它顯示爲0更改爲這樣的事情:

System.out.printf("%g ", q[index]); 

,你會得到:

Plantetary Data 
===================================== 
Planet Diameter Mass Gravity 
Mercury 5952136.1 3.30E+23 1.20225e-27 
Venus 36625493.6 4.87E+24 5.01781e-28 
Earth 40678884.0 5.97E+24 4.54212e-28 
Mars 11577006.3 6.42E+23 1.20306e-27 
Jupiter 5111106064.0 1.90E+27 1.79559e-28 
Saturn 3632472900.0 5.68E+26 4.26215e-28 
Uranus 653415844.0 8.68E+25 5.02049e-28 
Neptune 613751076.0 1.02E+26 3.99660e-28 
Pluto 1428025.0 1.27E+22 7.25985e-27