美文网首页
2019-05-23折线打印矩阵

2019-05-23折线打印矩阵

作者: 咣超 | 来源:发表于2019-05-23 15:24 被阅读0次
package 练习题;

public class Matrix3 { // 折线打印矩阵
    public static void printZagMatrix(int[][] m) {
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int endR = m.length - 1;
        int endC = m[0].length - 1;
        boolean f = true;
        while(a <= endR && d <= endC) {
            printProcess(m, a, b, c, d, f);
            a = b == endC ? a + 1 : a;
            b = b == endC ? b : b + 1;
            d = c == endR ? d + 1 : d;
            c = c == endR ? c : c + 1;
            f = !f;
        }
    }
    
    public static void printProcess(int[][] m, int a, int b, int c, int d, boolean f) {
        if(f) {
            while(c >= a) {
                System.out.println(m[c][d]);
                c--;
                d++;
            }
        }else {
            while(a <= c) {
                System.out.println(m[a][b]);
                a++;
                b--;
            }
        }
    }
    
    public static void main(String[] args) {
        int[][] matrix = {
                          {1, 2, 3}, 
                          {4, 5, 6}, 
                          {7, 8, 9}
                          };
        printZagMatrix(matrix);
        
    }
}

相关文章

网友评论

      本文标题:2019-05-23折线打印矩阵

      本文链接:https://www.haomeiwen.com/subject/nufbzqtx.html