我需要帮助将相同的数组与其自身相乘(对其进行平方)。我尝试复制它并创建一个新数组,但仍然收到类型不兼容的错误。该错误在 radius1 上突出显示,指出类型不兼容。
public static double[] part1(double[] z, double[] radius,double [] radius1)
{
for(int index=0; index <10; index++)
{
z= radius[index]*radius1[index];
}
// The formula to calculate gravity is:
// 6.67E-11 times the massOfPlanet divided by the radius of the planet squared
return z;
}
public static void main(String[] args)throws IOException
{
double[] radius = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
double[] radius1 = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
double z[]= new double [9];
}
最佳答案
考虑一下
z= radius[index]*radius1[index];
z
被声明为 double[]
。您正在使用表达式
radius
和 radius1
中的组件
radius[index] * radius1[index];
其计算结果为double
值。
您不能将 double
值分配给 double[]
类型的引用变量。
关于java - 将数组与其自身相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484285/