打印 ax,y 矩阵,java

发布于 2024-09-28 12:01:52 字数 466 浏览 4 评论 0原文

我想使用 for 循环打印以下矩阵:

 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

我使用:

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

但它打印:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无妨# 2024-10-05 12:01:52

将您的内循环更改为:

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

Change your inner loop to this:

for (int p = w; p <= 6 + w; p++) {
    System.out.print((p - 1) % 6 + 1 + " ");
}
万水千山粽是情ミ 2024-10-05 12:01:52

我认为使用模来打印每种可能的组合更容易。但是,这会返回 0 到 5 之间的数字,因此您必须加 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();
}

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();
}
多孤肩上扛 2024-10-05 12:01:52
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  + "");
        }
    }
}
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  + "");
        }
    }
}
又怨 2024-10-05 12:01:52

这应该可以帮助你。几乎就是你想要做的,但有一个额外的 for 循环。

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

    }
陌上芳菲 2024-10-05 12:01:52

这会起作用,虽然我还没有测试过!:

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

干杯

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

空‖城人不在 2024-10-05 12:01:52

发生这种情况是因为您的内部循环取决于 w,但 w 正在递增。

编辑——这是我想出的

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

}

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

}
悍妇囚夫 2024-10-05 12:01:52

其他人给出了奇特的模数解决方案,但我认为最简单的事情是有第二个内部循环来覆盖第一个内部循环错过的数字。

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

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("");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文