views:

88

answers:

7

I want to print the followin matrix using for loops:

 1 2 3 4 5 6
 2 3 4 5 6 1
 3 4 5 6 1 2
 4 5 6 1 2 3
 5 6 1 2 3 4
 6 1 2 3 4 5

I use:

public static void main ( String [ ] args ) {
    for(int w = 1; w <= 6; w++){
        System.out.println("");
        for(int p = w; p <= 6; p++){
            System.out.print(p + "");
        }
    }
}

But it prints:

 1 2 3 4 5 6
 2 3 4 5 6
 3 4 5 6
 4 5 6
 5 6
 6
+2  A: 
public static void main ( String [ ] args ) {
    for(int w = 0; w < 6; w++){
        System.out.println("");
        for(int p = 0; p < 6; p++){
                System.out.print((p + w) % 6 + 1  + "");
        }
    }
}
shoebox639
No, `p = w` is fine; you just need to adjust for it a different way.
Chris Jester-Young
Lol I totally misunderstood the question at first. Now that I got it, I've edited >.<
shoebox639
+1  A: 

this is happening because your inner loop depends on w, but w is incrementing.

edit -- here is what i came up with

public class Loop {
    public static void main(String[] args) {
        for (int w = 1; w <= 6; w++) {
            System.out.println("");
            Loop.printRow(w);

        }
    }

    public static void printRow(int startAt) {

        int p = startAt;
        for(int i = 0; i <= 6; i++, p++){
            if (p > 6) p -= 6;

            System.out.print(p + "");
        }
    }

}
hvgotcodes
+3  A: 

Change your inner loop to this:

for (int p = w; p <= 6 + w; p++) {
    System.out.print((p - 1) % 6 + 1 + " ");
}
Chris Jester-Young
+1  A: 

This will work, although I haven't tested it!:

for(int w = 1; w <= 6; w++){ 
    System.out.println("");
    for(int p = 0; p <= 5; p++){
       if((w+p) <=6) {
           System.out.print((w+p) + "");
       } else {
           System.out.print((w+p-6) + "");
       }
    } 
}

Cheers

Valchris
It needs a `% 6` somewhere to make the numbers wrap back to 1.
Chris Jester-Young
Yea, mod would work as well.
Valchris
+3  A: 

I think it is easier to work with modulo to print every possible combination. But, this returns a number between 0 and 5, so you have to add 1.

final int SIZE = 6;
for(int w = 0; w < SIZE; w++) {
  for(int p = 0; p < SIZE; p++) {
    System.out.print(((w + p) % SIZE) + 1);
    System.out.print(" ");
  }
  System.out.println();
}
Marc
+1  A: 

This should help you out. Almost what you are trying to do, but with an extra for loop.

public static void main(String[] args) {

        for (int w = 1; w <= 6; w++) {
            System.out.println("");

            for (int p = w; p <= 6; p++) {
                System.out.print(p + "");
            }
            for (int q = 1; q < w; q++) {
                System.out.print(q + "");
            }
        }

    }
johnbk
That doesn't give the same output. The numbers in the lower half should also be increasing.
Ken Bloom
@Ken - Thanks, edited my solution.
johnbk
+1  A: 

Other people have given fancy solutions with modulo, but I think the simplest thing is to have a second inner loop that covers the numbers the first inner loop missed.

public static void main ( String [ ] args ) {
    for(int w = 1; w <= 6; w++){
        for(int p = w; p <= 6; p++){
            System.out.print(p + "");
        }
        for(int p = 1; p < w; p++){
            System.out.print(p + "");
        }
        System.out.println("");
    }
}
Ken Bloom