简体   繁体   中英

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. You're attempting to convert a double to a double array.

Looking at your function, calcGravity, you're returning a result of:

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.

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. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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