我试图将X Y Z变量的数组或列表传递给计算欧氏距离的方法。
这是我的方法:
public static double calculateDistance(int[] array1, int[] array2)
{
double distance = 0.0;
for(int i=0;i<array1.length;i++) {
distance += Math.pow(array1[i]-array2[i], 2.0);
distance = Math.sqrt(distance);
}
return distance;
}
这是我的主要代码:
int b[] = {0, 0, 0};
int c[] = {10, 0, 0};
double distance = calculateDistance(b, c);
System.out.println(distance);
输出是:1.7782794 ..什么时候应该是10.任何想法?
答案 0 :(得分:3)
distance = Math.sqrt(distance)
错了:
public static double calculateDistance(double[] array1, double[] array2) {
double Sum = 0.0;
for (int i = 0; i < array1.length; i++) {
Sum = Sum + Math.pow((array1[i] - array2[i]), 2.0);
}
return Math.sqrt(Sum);
}
然后
double b[]={0,0,0};
double c[]={10,0,0};
double distance = calculateDistance(b, c);
System.out.println(distance);