Search code examples
javaloopsprocessing

How to arrange printed numbers using loops in processing? i.e. x and y axis in 10's up to a 100


Ok, I know how to print the number up to whatever number but I am having trouble rearranging them.

This is the format I want..

1   2   3   4   5   6   7   8   9   10

11  12  13  14  15  16  17  18  19  20


21  22  23... 

all the way to 100. so, a 10 x 10 grid format.

This is the code I used:

size(500,500);
int x, y;
x= 30;
y= 30;

for(int i=1; i<=100; i++){
    text(i, x, y);
    x+=30;
}

however, it counts to a 100 in a straight line, and I'm not sure how to make 11 appear on the second line.

I have tried if statements, but the result is limited and requires numerous if statements to carry out the function all the way to 100. what do I do and how can the code be more efficient?


Solution

  • When the counter is a multiple of 10, increase the y coordinate and move the x coordinate back to the start of the line.

    for(int i=1; i<=100; i++){
        text(i, x, y);
        x+=30;
        if(i % 10 == 0){
           x=30; 
           y += 30;
        }
    }