两个数组的乘积

huangapple go评论88阅读模式
英文:

Product of two arrays

问题

我是Java的初学者,我尝试创建一个程序,该程序接受两个数组,将两个数组中对应位置的数字相乘,并创建一个显示输出的第三个数组。我尝试过但一直遇到错误。

我创建了一个额外的方法来接受这两个数组并进行相乘,但我不确定是否正确创建了返回乘积的第三个数组。
对于如何解决这个问题或任何纠正意见,我将不胜感激。感谢您的时间!

public static void main(String[] args){
    int[] setA = {1,2,3,4,5};
    int[] setB = {2,4,6,7,8};
    int[] product = arrayProduct(setA, setB);
    System.out.print("The product is: ");
    for (int i = 0; i < product.length; i++) {
        System.out.print(product[i] + " ");
    }
}

public static int[] arrayProduct(int[] arrayA, int[] arrayB){
    int[] product = new int[arrayA.length];
    
    for (int i = 0; i < arrayA.length; i++) {
        int num1 = arrayA[i];
        int num2 = arrayB[i];
        product[i] = num1 * num2;
    }
    return product;
}

这是一个修复了错误的版本,它将两个数组相应位置的数字相乘,并正确创建并返回了第三个数组。希望这对你有所帮助!

英文:

i'm a beginner at java and i'm trying to create a program that takes two arrays, and multiplies each corresponding number in both arrays and creates a third array that displays the output. I've attempted and keep running into errors.

I've created an additional method that takes the arrays and multiplies them, however i'm not sure if i've created the third array correctly that returns the product.
Any help or insight into how to approach this problem or any corrections is much appreciated. Thanks for your time!

    public static void main(String[] args){
	int[] setA = {1,2,3,4,5};
	int[] setB = {2,4,6,7,8};
	arrayProduct(setA, setB);
	System.out.print(&quot;The product is: &quot; + int[] product);
	
}

public static int[] arrayProduct(int[] arrayA, int[] arrayB){
	int[] product = {};
	int i;
	
	for (i = 0; i &lt; arrayA.length; i++) {
		int num1 = arrayA[i];
		int num2 = arrayB[i];
		product += Integer.toString(num1 * num2) + &quot; &quot;;
		
	}
	return int[] product;
	
} 

答案1

得分: 1

public static void main(String[] args){
    int[] setA = {1,2,3,4,5,6};
    int[] setB = {2,4,6,7,8};

    //调用arrayProduct方法,该方法需要两个int数组int[]作为参数。将返回的int[]存储在名为productArray的新int[]中。
    int[] productArray = arrayProduct(setA, setB);

    //在一行上显示整个数组。
    System.out.print("The product is: " + Arrays.toString(productArray));
}

//如果arrayProduct()仅在其自己的类内部访问,请将该方法设置为private。
private static int[] arrayProduct(int[] arrayA, int[] arrayB){

    //在创建product[]数组之前,可以在此处执行一些验证,以确保两个数组具有相同的长度,如果arrayA.length = 7,arrayB.length = 6,
    //则在尝试获取arrayA[6] * arrayB[6]的乘积时会引发IndexOutOfBoundsException。
    int[] product = new int[arrayA.length];

    for (int i = 0; i < arrayA.length; i++) {
        product[i] = arrayA[i] * arrayB[i];

    }

    return product;
}

如果你是Java中的数组新手,请查看以下链接以获得基本了解:https://www.w3schools.com/java/java_arrays.asp

英文:
public static void main(String[] args){
    int[] setA = {1,2,3,4,5,6};
    int[] setB = {2,4,6,7,8};

    //Invoke the arrayProduct method which requires 2 int arrays int[] as parameters. Store the returned int[] in a new int[] called productArray.
    int[] productArray = arrayProduct(setA, setB);

    //Display the whole array on one line.
    System.out.print(&quot;The product is: &quot; + Arrays.toString(productArray));

}

//If arrayProduct() is only accessed from within its own class, set the method to private. 
private static int[] arrayProduct(int[] arrayA, int[] arrayB){

    //You could do some validation here before creating a product[] array to ensure both arrays are of the same length, if arrayA.length = 7 and arrayB.length = 6
    //you will get a IndexOutOfBoundsException... when trying to get the product of arrayA[6] * arrayB[6].
    int[] product = new int[arrayA.length];

    for (int i = 0; i &lt; arrayA.length; i++) {
        product[i] = arrayA[i] * arrayB[i];

    }

    return product;

}

If you are new to Arrays in java check out the following link to gain a basic understanding https://www.w3schools.com/java/java_arrays.asp

huangapple
  • 本文由 发表于 2020年5月4日 14:56:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61586694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定