我試圖計算和顯示陣列我的程序,但它口口聲聲說像你不能轉換雙爲雙[],例如,在我的代碼,當我計算表面重力,它說,我不喜歡有一個數組和可變的混合,因爲我聲明瞭萬有引力常數作爲變量,一切公式作爲數組中的其他。另一個是當我除以2的直徑陣列計算半徑,並通過半徑變量作爲用於計算表面重力的靜態方法的參數時,編譯器說,一個雙不能被轉換爲雙[]因爲在用於表面重力的靜態方法,我聲明瞭半徑爲一個數組。任何幫助將不勝感激。以下是我的代碼:Java靜態方法和使用數組計算
public class GravityV1{
// calculate surface gravity static method
public static double surfaceGravity(double G, double[] M, double[] r){
// equation for surface gravity
double[] g = (G * M)/Math.pow(r, 2);
// return statement
return g;
}
// displaying static method
public static void display(String[] planets, double[] radius, double[]
mass, double[] g){
// display output
System.out.printf("%n", planets, radius, mass, g);
}
// write results to text file static method
public static void main(String args[]){
// declare array for months
String[] Planets = new String[8];
// initialize array
Planets [0] = "Mercury";
Planets [1] = "Venus";
Planets [2] = "Earth";
Planets [3] = "Mars";
Planets [4] = "Jupiter";
Planets [5] = "Saturn";
Planets [6] = "Uranus";
Planets [7] = "Neptune";
// declare array for mass
double[] mass = new double[8];
// initialize array
mass [0] = 3.30E23;
mass [1] = 4.869E24;
mass [2] = 5.97E24;
mass [3] = 6.4219E23;
mass [4] = 1.900E27;
mass [5] = 5.68E26;
mass [6] = 8.683E25;
mass [7] = 1.0247E26;
// declare array for diameter
double[] diameter = new double[8];
// initialize array
diameter [0] = 4880;
diameter [1] = 12103.6;
diameter [2] = 12756;
diameter [3] = 6794;
diameter [4] = 142984;
diameter [5] = 120536;
diameter [6] = 51118;
diameter [7] = 49532;
// // convert to radius
// declare radius variable
double radius;
// for each loop for displaying
for(double calc : diameter){
// calculation for converting to radius
radius = calc/2;
}
// // calculate surface gravity
// declare and initialize universal gravity constant;
double G = 6.67384 * Math.pow(10, -11);
// call surface gravity method
surfaceGravity(G, mass, radius);
}
}