java静态方法和数组计算

时间:2013-11-15 19:32:15

标签: java arrays methods static

我正在尝试为我的程序计算和显示数组,但它一直在说像你不能将double转换为double [],例如,在我的代码中,当我计算表面引力时,它说我我不喜欢混合使用数组和变量,因为我将通用引力常量声明为变量,将等式中的所有其他内容声明为数组。另一个是当我通过将直径数组除以2来计算半径,并将半径变量作为参数传递给静态方法来计算表面重力时,编译器说双精度不能转换为double [],因为在表面引力的静态方法,我将半径声明为数组。任何帮助将不胜感激。以下是我的代码:

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

3 个答案:

答案 0 :(得分:0)

是的,您不能将标量与数组混合使用。您需要重写此方法:

public static double[] surfaceGravity(double G, double[] M, double[] r){

    double[] g = new double[M.length];
    // equation for surface gravity
    for (int i=0; i < g.length; i++) {
        g[i] = (G * M[i]) / Math.pow(r[i], 2);
    }

    // return statement
    return g;
}

答案 1 :(得分:0)

你应该逐个计算双数组中的元素。

更改

// 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;
}

// calculate surface gravity static method
public static double[] surfaceGravity(double G, double[] M, double[] r){

    // equation for surface gravity
    double[] g = new double[M.length];

    for(int i=0;i<g.length;i++)
    {
        g[i] = (G * M[i]) / Math.pow(r[i], 2);
    }

    // return statement
    return g;
}

答案 2 :(得分:0)

完成其他答案后,您应该建立一个半径数组(因为surfaceGravity需要double[]作为其第三个参数):

double[] radius = new double[diameter.length];

// for each loop for displaying
for(int i = 0; i < diameter.length; i++) { 

    // calculation for converting to radius
    radius[i] =  calc / 2;
}