开发者

Print a x,y matrix, java

开发者 https://www.devze.com 2023-01-21 09:07 出处:网络
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

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
 开发者_如何学Python2 3 4 5 6
 3 4 5 6
 4 5 6
 5 6
 6


Change your inner loop to this:

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


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();
}


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  + "");
        }
    }
}


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 + "");
            }
        }

    }


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


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 + "");
        }
    }

}


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("");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消