Hubi
Hubi

Reputation: 560

Is there a best approach to create four times 1 - 12 blocks through a loop

There is a loop that increments the counter 48 times to write certain values to an Excel file. In the range 1 - 48, 4 blocks from 1 - 12 are to be written.

Expected example: 1 2 3 4 5 6 7 8 9 10 11 12 - 1 2 3 4 5 6 7 ... and so on (4 times).

I have tried different approaches, if/else, switch/case but here I have not come to any result. My last approach is an if condition with the modolu operator.

        for (int i = 1; i <= 48; i++) {
        if (i % 12 != 0) {
            for (int j = 1; j <= 12; j++) {
                workBook.setNumber(HEADLINE_ROW, i + 6, j);
            }
        } else {
            workBook.setNumber(HEADLINE_ROW, i + 6, 12);
        }
    }

But with this approach I get 12 12 12 12 and so on. I recognize the error, but currently have no idea how to solve the problem. The part where data is written to the Excel file is rather unimportant. I am concerned with the logic.

I'm stuck in the logic here and can't get any further. Do you have any ideas or suggestions for improvement on how I can generate four 1 - 12 blocks side by side?

Upvotes: 0

Views: 32

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521997

I think the pseudo code for what you want would be:

for (int i=1; i <= 48; i++) {
    int j = i % 12 + 1;  // 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 ...
    // do something with i and (j + 1)
}

That is, work with the outer loop counter mod 12, which would give you the sequence 1, 2, ..., 12, four times.

Upvotes: 2

Amir Feldman
Amir Feldman

Reputation: 115

do something like that

python

for i in range(48):
  index = i % 12 + 1
  # do what ever you want here
  print(index)

java

for(int i = 0; i < 48; i++) {
    int index = i % 12 + 1;
    // do something here
}

Upvotes: 2

Related Questions