简体   繁体   中英

Is there a way to loop through 2 for loops, such that (i+j) > 3

I have an issue with a hexagonal matrix system. I want to loop through 2 arrays of length 6, but I need to exclude the "rhombus" combinations from the hexagon. So that means that the value of i+j needs to be greater than 3 and lower than 13. Here is some code so you might get the idea better:

public static void main(String args[]) {
    int i, j;
    for(i=0; (i+j)>3 && i<9; i++) {
        for (j=0; (i+j)>3 && j<(9); j++) {
            System.out.println(i);
            System.out.println(j);
        }
    }
}

So i want the output to be 0,4 , 1,3 , 2,2 , 3,1 , 4,0 and everything above until 4,8 , 5,7 , 6,6 , 7,5 , 8,4

I hope that this is clear enough, and yes I get that what I have will never output what I expect it to do, but that's the whole problem :)

Don't over-complicate the for-loops.

public static void main(String args[]) {
  for(int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      if ((i + j) > 3 && (i + j) < 13) {
        System.out.println(i + ", " + j);
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM