【问题标题】:Performing calculations with arrays使用数组执行计算
【发布时间】:2013-12-11 15:09:36
【问题描述】:

我有一个关于如何使用数组执行计算的问题。就我而言,我需要执行数组的计算,然后在 main 方法中调用它。我在返回类型中进行了计算,编译器抱怨 double 无法转换为 double[] 所以我尝试获取数组的长度,但仍然收到相同的警告。我怎样才能解决这个问题?任何帮助将不胜感激。以下是我的代码:

// calcGravity returns an array of doubles containing teh gravity values
//and takes two arrays of doubles as parameters for the radius values and mass
public static double[] calcGravity(double[] radius, double[] mass)
{
    // fill in code here
        return (6.67E-17) * mass.length / Math.pow(radius.length, 2);
    // The formula to calculate gravity is:
    // 6.67E-17 times the massOfPlanet divided by the radius of the planet squared
}

【问题讨论】:

标签: java arrays methods return


【解决方案1】:
public static double[] calcGravity(double[] radius, double[] mass)
{
    double[] ret = new double[radius.length];
    for (int i=0;i<radius.length;i++) {
      // fill in code here
      ret[i] = (6.67E-17) * mass[i] / Math.pow(radius[i], 2);
      // The formula to calculate gravity is:
      // 6.67E-17 times the massOfPlanet divided by the radius of the planet squared
   }
   return ret;
}

【讨论】:

    【解决方案2】:
    public static double[] calcGravity(double[] radius, double[] mass){
       double[] grav = new double[radius.length]();
       for (int i =0; i<radius.length;i++) {
          grav[i] = (6.67E-17) * mass[i] / Math.pow(radius[i], 2);
       }
       return grav;
    }
    

    【讨论】:

      【解决方案3】:

      您需要单独对每一行进行计算。

      double[] gravities = new double[radius.length]();
      
      for (int i =0; i<radius.length;i++) {
        gravities[i] = (6.67E-17) * mass[i] / Math.pow(radius[i], 2);
      }
      return gravities;
      

      【讨论】:

        【解决方案4】:

        double[] 是一个双精度数组。你只是在计算一个数字。当您尝试返回 double 并且返回类型为 double[] 时,编译器无法对其进行转换。

        你的计算是错误的。 radiusmass 的长度不是半径和质量。它们每个都包含一堆数字,每个数字都是一个半径和一个质量。您想计算每个质量/半径对的每个重力并返回一个包含这些的新数组:

        public static double[] calcGravity(double[] radius, double[] mass)
        {
            // Check that you have the same number of radii as masses
            if (radius.length != mass.length)
              throw new IllegalArgumentException("Cannot calculate gravities with unequal radii and masses.");
        
            double[] gravity = new double[radius.length];
            for (int i = 0; i < radius.length; i++) {
              double[i] = (6.67E-17) * mass[i] / Math.pow(radius[i], 2);
            }
            return gravity;
        }
        

        【讨论】:

          【解决方案5】:

          如果您正在访问mass.lengthradius.length,您将获得这些数组中的元素数量。

          所以你必须编写一个循环来遍历这些数组中的所有值。

          public static double[] calcGravity(double[] radius, double[] mass) {
              int size = radius.length;
          
              if (size != mass.length) {
                  throw IllegalArgumentException("can't calculate with different count of array elements!");
              }
          
              double[] sums = new double[size];
          
              for (int i = 0; i < size; i++) {
                 sums[i] = (6.67E-17) * mass[i] / Math.pow(radius[i], 2);
              }
          
              return sums;
          }
          

          【讨论】:

            猜你喜欢
            • 2014-03-12
            • 1970-01-01
            • 2016-07-05
            • 1970-01-01
            • 2019-09-26
            • 2011-10-21
            • 2011-12-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多