我正在尝试执行我的最后一个方法,但不确定将我的主方法中的该方法需要添加什么。当前,我的程序执行第一个方法,然后执行第二个方法,然后程序结束。我对Java非常陌生,任何见解都将非常有帮助!感谢您查看我的代码。
import java.util.Arrays;
import java.util.Scanner;
public class dot {
static int n = 3;
public static void main(String[] args) {
getArrayOne();
getArrayTwo();
}
public static double[] getArrayOne() {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the length of your first vector :");
int lengthOne = Integer.parseInt(scan.nextLine());
double[] vOne = new double[lengthOne];
for (int i = 0; i < lengthOne; i++) {
System.out.println("Please enter a vector value of your first vector :");
double valuesOne = Double.parseDouble(scan.nextLine());
vOne[i] = valuesOne;
}
System.out.println("Your first vector values are: " + Arrays.toString(vOne));
return vOne;
}
public static double[] getArrayTwo() {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the length of your second vector :");
int lengthTwo = Integer.parseInt(scan.nextLine());
double[] vTwo = new double[lengthTwo];
for (int i = 0; i < lengthTwo; i++) {
System.out.println("Please enter a vector value :");
double valuesTwo = Double.parseDouble(scan.nextLine());
vTwo[i] = valuesTwo;
}
System.out.println("Your second vector values are: " + Arrays.toString(vTwo));
return vTwo;
}
public static double dotProduct(double vOne[], double vTwo[]) {
double product = 0.0;
for (int i = 0; i < n; i++) {
product = product + vOne[i] * vTwo[i];
}
System.out.println("Dot Product: ");
System.out.println(dotProduct(vOne, vTwo));
System.out.println(product);
return product;
}
}
答案 0 :(得分:1)
将主方法中的代码更改为
double[] arr1 = getArrayOne();
double[] arr2 = getArrayTwo();
double dotProduct = dotProduct(arr1, arr2);
问题是您实际上并未调用dotProduct
方法。
除此之外,您的代码中还有其他值得一提的东西。
您可以通过一些重构消除一些冗余。如果您更改逻辑以使用通用getArray(int, Scanner)
。这将帮助您使代码更易于维护。 int
类型参数将占用数组的长度,而Scanner
是您用来从键盘获取输入的扫描器。由于您要进行点/标量积运算,因此向量必须具有相同的长度。
您需要关闭创建的Scanner
对象,以防止内存泄漏。
您的dotProduct方法调用时不带任何基本情况(不受控制的递归)。那是个坏主意。
请考虑以下内容:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the length of the vectors whose dot product you wish to calculate :");
int length = Integer.parseInt(scan.nextLine());
System.out.println("Getting values for the first vector");
double[] arr1 = getArray(length, scan);
System.out.println("Getting values for the second vector");
double[] arr2 = getArray(length, scan);
System.out.println("Calculating the dot product");
double dotProduct = getDotProduct(arr1, arr2);
// TODO do something with the dot product
scan.close();
}
private static double[] getArray(int length, Scanner scan) {
double[] vector = new double[length];
for (int i = 0; i < length; i++) {
System.out.println("Please enter a vector value :");
double valuesOne = Double.parseDouble(scan.nextLine());
vector[i] = valuesOne;
}
System.out.println("Your vector values are: " + Arrays.toString(vector));
return vector;
}
private static double getDotProduct(double vOne[], double vTwo[]) {
double product = 0.0;
for (int i = 0; i < vOne.length; i++) {
product = product + vOne[i] * vTwo[i];
}
System.out.println("Dot Product: ");
System.out.println(product);
return product;
}
值得一读的另一件事是Java的编码约定。
答案 1 :(得分:0)
您需要调用第三个方法,并传递前两个方法的返回值
double[] array1 = getArrayOne();
double[] array2 = getArrayTwo();
double dotProduct = dotProduct(array1, array2);
一些改进当前代码的注释:
getArrayOne
和getArrayTwo
做同样的工作。因此,您可以将它们组合为一个函数,然后从main调用它。Scanner
对象。