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);
}
}
网友评论