我正在尝试创建一个程序来获取两个向量的点积。我通过创建一个数组列表(命名距离)来实现它。通过在main方法中更改此行,可以使该程序使用硬编码数字运行。
double vect_A[] = distnace(mag);
double vect_B[] = distance(dista);
我知道上面的代码无法运行,是否可以将这些特定值返回到单独的数组中?还是我错误地解决了这个问题。我对Java非常陌生,将不胜感激。
//java implementation for dot product
// and cross product of two vector.
import java.util.Scanner;
import java.util.ArrayList;
class practiceMats {
public static void main (String[] args) {
vector();
double vect_A[] = distnace(mag);
double vect_B[] = distance(dista);
// dotProduct function call
System.out.print ( "Dot product:");
System.out.println (dotProduct(vect_A, vect_B)) ;
}
static double n = 3.0;
// Function that return
// dot product of two vector array.
static double dotProduct(double vectorA[], double vectorB[])
{
double product = 0.0;
// Loop for calculate cot product
for (int i = 0; i < n; i++)
product = product + vectorA[i] * vectorB[i];
return product;
}
static ArrayList<Integer> vector()
{
ArrayList<Integer>distance= new ArrayList<Integer>();
Scanner scan = new Scanner (System.in);
System.out.println("Please enter your array length: ");
int s = Integer.parseInt(scan.nextLine());
System.out.println();
for(int i = 0; i < s; i++) {
System.out.println("Enter your distance: ");
int dista = Integer.parseInt(scan.nextLine());
distance.add(dista);
System.out.println("Enter your distance: ");
int mag = Integer.parseInt(scan.nextLine());
distance.add(mag);
}
return distance;
}
}
相关分类