计算值的总和数组 [英] Computing sum of values in array

查看:106
本文介绍了计算值的总和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题即时得到的是与oddSum输出的值是相同的evenSum以及所有元素的和的值为0

我不能挺看到我去错了,因为环路类似pretty,如果连一个工作的人也应该如此?

下面是我的code反正:

  INT evenData [] =新INT [10];
INT oddData [] =新INT [10];
INT总和= 0;
INT evenSum = 0;
INT oddSum = 0;INT []数据= {3,2,5,7,9,12,97,24,54};
对于(INT指数= 0;指数 - LT; data.length;指数++)
{
    如果(数据[指数]%2 == 0)
    {        INT TEMP =数据[指数]
        数据[指数] = evenData [指数]
        evenData [指数] =温度;    }    其他
    {
        INT TEMP =数据[指数]
        数据[指数] = oddData [指数]
        oddData [指数] =温度;
    }}
对(INT evenIndex = 0; evenIndex&下; evenData.length; evenIndex ++)
{    evenSum = evenData [evenIndex] + evenSum;}
System.out.print(偶数元素的总和:+ evenSum);对(INT oddIndex = 0; oddIndex&下; oddData.length; oddIndex ++)
{    oddSum = oddData [oddIndex] + oddSum;}
System.out.print(奇元素的总和:+ oddSum);对于(INT指数= 0;指数 - LT; data.length;指数++)
{
    总和=数据[指数] +总和;
}
System.out.print(所有元素的总和:+总和);


解决方案

您也越来越为相同的值甚至因为要打印相同的值: -

  System.out.print(奇元素的总和:+ evenSum);

另外,你的最后一笔是,因为你在做你原有的阵列中的所有元素为 ,因为你是在要素交换的元素 evenData oddData ,这是零开始。

  INT TEMP =数据[指数]
数据[指数] = evenData [指数] //这个code分配一个值0至当前索引。
evenData [指数] =温度;

所以,你迭代阵列,并指派 0 您的每一个指标,同时加入到新的previous元阵列


我会说,你是不必要使用2个额外阵列和3个额外的循环。为什么不直接在你迭代的原始数组的地方产生了一加?

在事实上,所有的款项可以在一个循环中计算的: -

 为(INT指数= 0;指数 -  LT; data.length;指数++)
{
    总和+ =数据[指数]    如果(数据[指数]%2 == 0)
    {
        // INT TEMP =数据[指数]
        //数据[指数] = evenData [指数]
        // evenData [指数] =温度;        evenSum + =数据[指数]
    }
    其他
    {
        // INT TEMP =数据[指数]
        //数据[指数] = oddData [指数]
        // oddData [指数] =温度;        oddSum + =数据[指数]
    }
}的System.out.println(连心:+ evenSum);
的System.out.println(奇总:+ oddSum);
的System.out.println(合计金额+总和);

所以,你不需要为甚至数字创造额外的数组。

和,也是你的 4回路现在已经凝结成只是一个单一的循环。

The problem im getting is that with oddSum the value outputted is the same as evenSum, and the value for sum of all elements is 0.

I cant quite see where im going wrong as the loops are pretty similar and if the even one works the others should too?

Here is my code anyway:

int evenData[] = new int [10];
int oddData[] = new int [10];
int sum = 0;
int evenSum = 0;
int oddSum = 0;

int[] data = {3, 2, 5, 7, 9, 12, 97, 24, 54};
for(int index = 0; index < data.length; index++)
{
    if (data[index] % 2 == 0)
    {

        int temp = data[index];
        data[index] = evenData[index];
        evenData[index] = temp;

    }

    else
    {
        int temp = data[index];
        data[index] = oddData[index];
        oddData[index] = temp;
    }

}
for(int evenIndex = 0; evenIndex < evenData.length; evenIndex++)
{

    evenSum =evenData[evenIndex] + evenSum;

}
System.out.print("Sum of even elements: " + evenSum);

for(int oddIndex = 0; oddIndex < oddData.length; oddIndex++)
{

    oddSum = oddData[oddIndex] + oddSum;

}
System.out.print("Sum of odd elements: " + oddSum);

for(int index = 0; index < data.length; index++)
{
    sum = data[index] + sum;
}
System.out.print("Sum of all elements: " + sum);

解决方案

You are getting same value for even and odd because you are printing the same value: -

System.out.print("Sum of odd elements: " + evenSum);

Also, your final sum is zero because you are making out all the elements of your original array as zero, as you are swapping your elements with the elements in evenData and oddData, which are zero initially.

int temp = data[index];
data[index] = evenData[index]; // This code assigns a value 0 to current index.
evenData[index] = temp;

So, you are iterating your array, and assigning 0 to each of your index, while adding the previous element to the new array.


I would say that you are needlessly using 2 extra array and 3 extra loops. Why not just create a sum in the place where you are iterating your original array?

In fact, all your sums can be computed in a single loop: -

for(int index = 0; index < data.length; index++)
{
    sum += data[index];

    if (data[index] % 2 == 0)
    {
        // int temp = data[index];
        // data[index] = evenData[index];
        // evenData[index] = temp;

        evenSum += data[index];
    }
    else
    {
        // int temp = data[index];
        // data[index] = oddData[index];
        // oddData[index] = temp;

        oddSum += data[index];  
    } 
}

System.out.println("Even Sum: "  + evenSum);
System.out.println("Odd Sum: "  + oddSum);
System.out.println("Total Sum: "  + sum);

So, you don't need to create extra arrays for even and odd numbers.

And, also your 4 loops have now been condensed to just a single loop.

这篇关于计算值的总和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆