簡體   English   中英

計算數組中值的總和

[英]Computing sum of values in array

我得到的問題是,用oddSum 輸出的值與evenSum 相同,並且所有元素之和的值為0。

我不太清楚我哪里出錯了,因為循環非常相似,如果偶數一個有效,其他的也應該?

無論如何,這是我的代碼:

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);

你得到相同的evenodd數值,因為你打印的是相同的值:-

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

此外,您的最終總和zero因為您將原始數組的所有元素都zero ,因為您將元素與evenDataoddData的元素交換,這些元素最初為零。

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

因此,您正在迭代數組,並為每個索引分配0 ,同時將前一個元素添加到new array


我會說你不必要地使用了 2 個額外的數組和 3 個額外的循環。 為什么不在迭代原始數組的地方創建一個總和?

事實上,您所有的總和都可以在一個循環中計算:-

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);

因此,您無需為evenodd創建額外的數組。

而且,您的4 loops現在也已濃縮為一個循環。

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

查看上面的行, evenDate 是空的,在第二行中,您將數據數組設置為空。 您也在oddDate 行中重復了同樣的錯誤。

你為什么不使用只是嘗試以下?

    for(int index = 0; index < data.length; index++)
    { 
        if (data[index] % 2 == 0) { 
            int temp = data[index]; 
            evenData[index] = temp;

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

    for(int evenIndex = 0; evenIndex < evenData.length; evenIndex++) 
    { 
        //since length of all 3 data,even, odd arrays are the same
        //you can put both sum operation in one for loop

        evenSum = evenData[evenIndex] + evenSum; 
        oddSum = oddData[evenIndex] + oddSum; 
        sum = data[evenIndex] + sum;  
    }

    System.out.print("Sum of even elements: " + evenSum); 
    //you have put evenSum for below odeUm printing in ur code
    System.out.print("Sum of odd elements: " + oddSum); 
    System.out.print("Sum of all elements: " + sum); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM