Java 如何打印此循环图案

Java 如何打印此循环图案,java,logic,Java,Logic,如何解决这种模式? 对我来说,它看起来像一个波浪 input:4 1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13 我设法找出了一些正确的数字,但我知道这还远远没有解决 这是我的密码 //asume g is from input int g = 4; for(int x = 1; x <=g;x++){ int d = x; for(int y = 1;y<=g;y++){ Syste

如何解决这种模式? 对我来说,它看起来像一个波浪

input:4

1 8 9  16
2 7 10 15
3 6 11 14
4 5 12 13
我设法找出了一些正确的数字,但我知道这还远远没有解决

这是我的密码

//asume g is from input
 int g = 4;

      for(int x = 1; x <=g;x++){
      int d = x;
        for(int y = 1;y<=g;y++){
          System.out.print(d+" ");
          d = g*x-y;
        }
        System. out. println();
      }

output:

1 3 2 1 
2 7 6 5 
3 11 10 9 
4 15 14 13 

//asume g来自输入
int g=4;

对于(intx=1;x,受@B.Go注释的启发,您可以实现以下代码片段:

代码片段

int g = 4;
for (int row = 1; row <= g; row++) {
    for (int col = 1; col <= g; col++) {
        if (col%2 == 1) {
            System.out.printf("%2d ", g*(col-1)+row);
        } else {
            System.out.printf("%2d ", g*col-row+1);
        }
    }
    System.out.println();
}
 1  8  9 16 
 2  7 10 15 
 3  6 11 14 
 4  5 12 13 

受@B.Go评论的启发,您可以实现以下代码片段:

代码片段

int g = 4;
for (int row = 1; row <= g; row++) {
    for (int col = 1; col <= g; col++) {
        if (col%2 == 1) {
            System.out.printf("%2d ", g*(col-1)+row);
        } else {
            System.out.printf("%2d ", g*col-row+1);
        }
    }
    System.out.println();
}
 1  8  9 16 
 2  7 10 15 
 3  6 11 14 
 4  5 12 13 

请找到一个基本的解决方案,这不是一个好方法

但它会给出正确的结果

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static List<ArrayList<Integer>> pattern(int j) {
        List<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        boolean loopComp = true;
        int resultArrayIndex = 0;
        int toAddWithResultForAntiClock = j - 1;
        int resultToPut = 0;
        for (int i = 0; i <= j * j - 1; i++) {
            resultArrayIndex = i % j;
            if (resultArrayIndex == 0) {
                loopComp = !loopComp;
            }
            if (loopComp) {
                resultToPut = i + 1 + toAddWithResultForAntiClock;
                toAddWithResultForAntiClock = toAddWithResultForAntiClock - 2;
            } else {
                resultToPut = i + 1;
            }
            if (toAddWithResultForAntiClock == -(j + 1)) {
                toAddWithResultForAntiClock = j - 1;
            }
            try {
                result.get(resultArrayIndex);
            } catch (Exception e) {
                ArrayList<Integer> a = new ArrayList<>();
                result.add(a);
            }
            result.get(resultArrayIndex).add(resultToPut);
        }
        return result;
    }

    public static void main(String[] args) {
        List<ArrayList<Integer>> result = pattern(4);
        for (ArrayList<Integer> arrayList : result) {
            System.out.println(arrayList);
        }
    }
}

请找到一个基本的解决方案,这不是一个好方法

但它会给出正确的结果

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static List<ArrayList<Integer>> pattern(int j) {
        List<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        boolean loopComp = true;
        int resultArrayIndex = 0;
        int toAddWithResultForAntiClock = j - 1;
        int resultToPut = 0;
        for (int i = 0; i <= j * j - 1; i++) {
            resultArrayIndex = i % j;
            if (resultArrayIndex == 0) {
                loopComp = !loopComp;
            }
            if (loopComp) {
                resultToPut = i + 1 + toAddWithResultForAntiClock;
                toAddWithResultForAntiClock = toAddWithResultForAntiClock - 2;
            } else {
                resultToPut = i + 1;
            }
            if (toAddWithResultForAntiClock == -(j + 1)) {
                toAddWithResultForAntiClock = j - 1;
            }
            try {
                result.get(resultArrayIndex);
            } catch (Exception e) {
                ArrayList<Integer> a = new ArrayList<>();
                result.add(a);
            }
            result.get(resultArrayIndex).add(resultToPut);
        }
        return result;
    }

    public static void main(String[] args) {
        List<ArrayList<Integer>> result = pattern(4);
        for (ArrayList<Integer> arrayList : result) {
            System.out.println(arrayList);
        }
    }
}

我的建议是:创建一个二维数组,用适当的值填充。只有这样做了,你才能按正确的顺序打印它们,行数从0开始,分别是1+行、2*g-行、2*g+1+行、4*g-行。如果列末尾的数字大于10,你应该在小于10的数字上添加两个空格。要解析波形输出,只需b在这个例子中,很难说它是如何生成的(可以是行和
34
30+g
))顺便说一句,您的问题与“设计模式”完全无关标记…我的建议:创建一个二维数组,用适当的值填充。只有这样做了,你才能按正确的顺序打印它们,行数从0开始,分别是1+行、2*g-行、2*g+1+行、4*g-行。如果列末尾的数字大于10,你应该为小于10的数字添加两个空格。要解析波形输出,请t仅通过该示例,很难说它是如何生成的(可以是行和
34
30+g
))顺便说一句,您的问题与“设计模式”完全无关标记…是的,它工作了,所以这仍然是一个非常好的答案。谢谢你,先生。我将分析代码,直到我了解它如何工作的过程。是的,它工作了,所以这仍然是一个非常好的答案。谢谢你,先生。我将分析代码,直到我了解它如何工作的过程。谢谢你,先生。我没想到会有这么短的代码。太棒了!顺便问一下,你是怎么做到的你培养你解决问题的能力吗?@Jerbx不客气!我认为比编码更重要的是观察。如果你遵守规则,比如在数学中将答案写成函数,那么它总是可以转化为代码的!谢谢你,先生。我没想到会有这么短的代码。太棒了!顺便问一下,你是如何构建你的问题解决方案的翻译技巧?@Jerbx不客气!我认为比编码更重要的是观察。如果你观察了规则,比如在数学中将答案写成函数,那么它总是可以转换成代码的!