简体   繁体   English

如何将双精度数组转换为双精度数组?

[英]How to convert a double to double array?

When I do this code, it tells me: 当我执行此代码时,它告诉我:

"incompatible: double cannot be converted to double[]"

What do I do to fix it? 我该如何解决?

public class Gravity
{
   public static double[] calcGravity(double[] radius, double[] mass)
   {
      // The formula to calculate gravity is: g = (G*M)/r^2
      for(int i = 0; i < 9; i++)
      {
         return 6.67E-17 * mass[i] / Math.pow(radius[i], 2); //incompatible error
      }

   public static void main(String[] args) throws IOException
   {
      // Initialize variables
      string[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
      double[] radii = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
      double [] masses = {3.30E23, 4.87E24, 5.97E24, 6.42E23, 1.90E27, 5.68E26, 8.68E25, 1.02E26, 1.27E22}; 

      // Processing
      double[] gravities = calcGravity(radii, masses);

      // Output
      printResults(names, radii, masses, gravities);
      printToFile(gravities);
    }
}

I'm not fresh on Java by any measure, but your compiler error is telling you exactly what it sounds like. 无论如何,我对Java都不陌生,但是您的编译器错误告诉您它的确切含义。 You're attempting to convert a double to a double array. 您正在尝试将double转换为double数组。

Looking at your function, calcGravity, you're returning a result of: 查看您的函数calcGravity,您将返回以下结果:

6.67E-17 * mass[i] / Math.pow(radius[i], 2); 6.67E-17 * mass [i] / Math.pow(radius [i],2);

Now, the problem is this. 现在,问题是这样。 You're using a return statement in a for loop, which will return a single double value. 您在for循环中使用return语句,该语句将返回单个double值。

I suppose what you're intending to do is make this calculating for each of the 9 calculations you're trying to make in calcGravity. 我想您打算对calcGravity中要进行的9个计算中的每一个进行此计算。 Therefore, what you need to do is create an array of whatever size you need, run through your for loop and set each value, and then return the array, not the individual calculation values. 因此,您需要做的是创建所需大小的数组,遍历for循环并设置每个值,然后返回该数组,而不是返回各个计算值。

public static double[] calcGravity(double[] radius, double[] mass)
{
    // The formula to calculate gravity is: g = (G*M)/r^2
    double[] array = new double[9]; 

    for(int i = 0; i < 9; i++)
    {
        array[i] =  6.67E-17 * mass[i]/ Math.pow(radius[i],2);              //incompatible error
    }

    return array;
}

Not sure if that's valid Java syntax for creating an array, but I think you get the point. 不确定用于创建数组的Java语法是否有效,但是我认为您已经明白了。

A function can only return only one value in a single call. 一个函数在一个调用中只能返回一个值。 Once it returns a value, the control goes back to the calling function. 返回值后,控件将返回到调用函数。 However in your code you are attempting to return more than one values ie, the value of gravity after each iteration. 但是,在您的代码中,您试图返回多个值,即每次迭代后的重力值。 i suggest you modify your function as follows: 我建议您如下修改功能:

public static void main(String[] args)throws IOException
{
// Initialize variables
String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
double[] radii = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
double [] masses = {3.30E23, 4.87E24, 5.97E24, 6.42E23, 1.90E27, 5.68E26, 8.68E25, 1.02E26, 1.27E22}; 
double gravities[]=new Double[9];
for(int i=0, i<9; i++)
{
   gravities[i] = calcGravity(radii[i] , masses[i] );
}
}
public static double calcGravity(double radius, double mass)
{
// The formula to calculate gravity is: g = (G*M)/r^2

for(int i = 0; i < 9; i++)
{
    return 6.67E-17 * mass/ Math.pow(radius,2);             
}
}//end of calcGravity

Also you had forgotten to close the calcGravity function. 您还忘记了关闭calcGravity函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM