java方法使用循环将数组中的元素与元素相乘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23521967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:33:20  来源:igfitidea点击:

java methods multiplying elements with in an array using a loop

javaarraysloopsfor-loopint

提问by n94pro

De?ne a Java method named weightedSum() that takes two integer arrays as its arguments. The method uses a loop to multiply corresponding elements of the two arrays together (i.e., it multiplies the ?rst argument of each array together, followed by multiplying the second element of each array together, and so on) and returns the sum of those products (which is also an integer). You may assume that both arrays are of equal length.

定义一个名为 weightedSum() 的 Java 方法,它接受两个整数数组作为其参数。该方法使用循环将两个数组的相应元素相乘(即,它将每个数组的第一个参数相乘,然后将每个数组的第二个元素相乘,依此类推)并返回这些乘积的总和(这也是一个整数)。您可以假设两个数组的长度相等。

public int weightedSum(int [] a ,int [] b)


    {
        int value;
        int sum ;

        for (int i = 0 ; i < a.length ; i++)
        {
            value = a[i] * b [i];
            value  = value +value ;

        }

        return value;

I am having trouble writing this method out for my assignment . I under stand it accepts to arrays but i am having trouble writing out the loop itself so that it multiplies each individual element of the array with its counterpart in the opposite array so pos [1] * pos [1] and then add the two values togather with pos [2] + pos[2] and to get the sum total for all the values

我在为我的作业编写此方法时遇到了麻烦。我理解它接受数组,但我无法写出循环本身,以便它将数组的每个单独元素与相反数组中的对应元素相乘,因此 pos [1] * pos [1] 然后将两个值相加与 pos [2] + pos[2] 一起得到所有值的总和

回答by Kakarot

below are the changes needed in your code. Basically you never updated the variable sum after you compute the product of corresponding elements in the two arrays. Also you might wanna use long type to store the result of the sum & the product as the int value might overflow if the elements in the array are sufficiently large.

以下是您的代码中所需的更改。基本上,在计算两个数组中相应元素的乘积后,您永远不会更新变量 sum。此外,您可能想要使用 long 类型来存储总和和乘积的结果,因为如果数组中的元素足够大,则 int 值可能会溢出。

public long weightedSum(int [] a ,int [] b)
{
    long value = 0;
    long sum = 0 ;

    for (int i = 0 ; i < a.length ; i++)
    {
        value = a[i] * b [i];
        sum = sum +value ;

    }

    return sum;
}

回答by peter.petrov

Change to this:

改成这样:

value += a[i] * b [i];
// value = value + value;

Also initialize valueto 0before using it. Also remove the sumvariable.

在使用它之前也初始化value0。同时删除sum变量。

回答by duffymo

Try this:

尝试这个:

public long dotProduct(int [] a, int [] b) {
    if (a == null) throw new IllegalArgumentException("a array cannot be null");
    if (b == null) throw new IllegalArgumentException("b array cannot be null");
    if (a.length != b.length) throw new IllegalArgumentException("arrays must have equal lengths");
    long sum = 0L;
    for (int i = 0; i < a.length; i++) {
        sum += a[i]*b[i];
    }
    return sum;
}

回答by Narble

Most of the code looks correct, however the second line in the loop should not be changing the value variable, the sum variable should be updated with the value. something like sum += value. then return sum.

大多数代码看起来是正确的,但是循环中的第二行不应该更改 value 变量,应该使用 value 更新 sum 变量。像 sum += value 之类的东西。然后返回总和。