'For' Loop Within a 'For' Loop and Then Another?

Posted by BenjaminFranklin on Stack Overflow See other posts from Stack Overflow or by BenjaminFranklin
Published on 2013-11-03T09:26:57Z Indexed on 2013/11/03 9:53 UTC
Read the original article Hit count: 133

Filed under:

This has confused me quite a bit, but it's also really interesting!

I want to loop through a grid of 9 elements in an array, multiply them all by 1/9. Then, I want to find the sum of those 9 elements and replace each individual element's with the value calculated as the sum. After I've done this, I want to move on to the next nine elements. To clarify, I want all 9 elements to be changed to myVal, in the code below.

So far I've got the loop within a loop bit, but I don't know how to then go back and replace each of the values with the sum of all of them combined. Here's my code:-

 previousx = 0; 
 previousy= 0;
for (int x = previousx; x < previousx+4; x++) {
    for(int y = previousy; y < previousy+4; y++) {
        y = y*(1/9);
        yVal += y;
      }
 }

Any advice would, of course, be greatly appreciated.

© Stack Overflow or respective owner

Related posts about java