I am trying to add every 12 numbers in an array. For example
double[] addMe = {147.04, 147.66, 148.27, 148.89, 149.51, 150.13, 150.76, 151.39, 152.02, 152.65, 153.29, 153.29,
10, 20 ,30,40,50,60,70,80,90,100,110,120};
I am able to add all the numbers inside the array by doing the following
double sum = 0;
for (double i : addMe){
sum += i;
}
System.out.println(sum);
Which gives me 2584.9.
But I am trying to get sum of every 12 numbers in the array which should give me
1804.84
780.0
How should I do that?
thanks.
解决方案
double sum=0.0;
for(int i=0;i
{
if(i%12==0 && i!=0)
{
System.out.println(sum);
sum=0;
}
sum +=addMe[i];
}