我需要帮助将相同的数组与自身相乘(平方)。我尝试复制它并创建一个新数组,但我仍然得到不兼容类型的错误。该错误在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];
}
答案 0 :(得分:1)
考虑一下
z= radius[index]*radius1[index];
z
被声明为double[]
。您正在使用表达式
radius
和radius1
中的组件
radius[index] * radius1[index];
评估为double
值。
您无法将double
值分配给double[]
类型的引用变量。
答案 1 :(得分:0)
我认为你的意思是
z[index] = radius[index] * radius1[index];
z
是一个数组,而不是double
。
答案 2 :(得分:0)
如果您只想对其进行平方,则无需复制数组,只需将每个条目与其自身相乘,并将其存储在您阅读的位置。
public static double[] squareArray(double[] arr) {
for (int i=0; i < arr.length; i++) {
arr[i] = arr[i] * arr[i];
}
return arr;
}
此外,您使用index < 10
作为条件,但最好通过读取输入数组arr
的长度(元素数)使其动态化。
答案 3 :(得分:0)
您收到错误是因为您试图将变量z(这是一个数组)声明为乘以两个双精度(也是一个双精度数)的乘积。如果您希望z中的每个INDEX都具有这两个元素的乘积,请将z [index]而不是z设置为该产品。
答案 4 :(得分:0)
Z是一个数组,radius[index]*radius1[index]
的结果是双
更改为
z[index] = radius[index]*radius1[index]
答案 5 :(得分:0)
您不需要两个数组来保存“radius”值。你可以将它们保存在一个数组中。
此外,z是一个数组,而不是双数。
public static double[] part1 (double[] z, double[] radius)
{
for (int index = 0; index < 10; index++)
{
z[index] = radius[index] * radius[index];
}
return z;
}
答案 6 :(得分:0)
试试这个
public static double[] part1(double[] radius,double [] radius1)
{
double[] z = new double[radius.length];
for(int index=0; index <10; index++)
{
z[index]= radius[index]*radius1[index];
}
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[]= par1(radius, radius1);
}
答案 7 :(得分:0)
您需要做的是更改for循环中的类型z
z[index]= radius[index]*radius1[index];
你忘了你已经声明为数组
答案 8 :(得分:0)
您应该在方法part1
中创建一个数组并将其返回。此外,最好使用radius.length
而不是硬编码的数字作为循环终止条件。在您的情况下,10是不正确的,因为radius
的长度是9。
import java.util.Arrays;
public class ArrayTest {
public static double[] part1(double[] radius, double[] radius1) {
double[] z = new double[radius.length];
for (int index = 0; index < radius.length; index++) {
z[index] = 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) {
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[] = part1(radius, radius1);
System.out.println(Arrays.toString(z));
}
}
答案 9 :(得分:0)
您正在尝试将z
double[]
设置为两个double
的乘积,其值为double
。在Java中不允许将数组设置为单个值。
此外,您的for
循环10次,即使您的数组只包含9个值,这会产生ArrayIndexOutOfBoundsException
。