【问题标题】:java static methods and calculation with arraysjava静态方法和数组计算
【发布时间】:2013-11-29 07:41:50
【问题描述】:

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

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

【问题讨论】:

    标签: java arrays methods static


    【解决方案1】:

    完成其他答案后,您应该建立一个半径数组(因为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;
    }
    

    【讨论】:

      【解决方案2】:

      你应该一个一个地计算双精度数组中的元素。

      改变

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2013-12-31
          • 1970-01-01
          • 1970-01-01
          • 2013-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-06
          • 2015-03-14
          相关资源
          最近更新 更多