当前位置: 代码迷 >> 综合 >> 二维数组回形输出
  详细解决方案

二维数组回形输出

热度:57   发布时间:2024-01-25 19:36:59.0
public class 二维数组回形输出 {public static void main(String[] args) {int[][] p = new int[4][5];for (int i = 0; i < p.length; i++) {for (int j = 0; j < p[i].length; j++) {int k = new Random().nextInt(1000);p[i][j] = k;System.out.print(k + " ");}System.out.println();}System.out.println();int row = p.length, col = p[0].length;int max = col * row;//输出结束则停止输出int times = 0;for (int n = 0; ; n++) {for (int j = n; j < col - n; j++) {//输出回形的上面的行System.out.println(p[n][j]);times++;}if (max == times) break;for (int i = n + 1; i < row - n; i++) {//输出回形的右边的列System.out.println(p[i][col - n - 1]);times++;}for (int j = col - n - 2; j >= n; j--) {System.out.println(p[row - n - 1][j]);//输出回形的下面的行times++;}if (max == times) break;for (int i = row - n - 2; i > n; i--) {//输出回形的左边的列System.out.println(p[i][n]);times++;}if (max == times) break;}}}
  相关解决方案