⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 juzhen.java~13~

📁 我做的一个螺旋矩阵程序
💻 JAVA~13~
字号:
package luxuan;

import java.io.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Juzhen {
  static int n;
  int stepX, stepY;
  int x = 0, y = n;
  int count = 0;
  int[][] ju = new int[n][n];
  public void fillfromUptoDown(int step) {
    for (int i = 0; i < step; i++) {
      if (ju[x][y] == 0) {
        ju[x][y] = ++count;
      }
      else {
        y++;
        ju[x][y] = ++count;
      }
    }
  }

  public void fillfromRighttoLeft(int step) {
    for (int i = 0; i < step; i++) {
      x--;
      ju[x][y] = ++count;
    }
  }

  public void fillfromDowntoUp(int step) {
    for (int i = 0; i < step; i++) {
      y--;
      ju[x][y] = ++count;
    }
  }

  public void fillfromLefttoRight(int step) {
    for (int i = 0; i < step; i++) {
      x++;
      ju[x][y] = ++count;
    }
  }

  public void make() {
    stepX = n;
    stepY = n - 1;
    while (count != n * n) {
      if (count != n * n) {
        fillfromUptoDown(stepY--);
      }
      if (count != n * n) {
        fillfromRighttoLeft(stepX--);
      }
      if (count != n * n) {
        fillfromDowntoUp(stepY--);
      }
      if (count != n * n) {
        fillfromLefttoRight(stepX--);
      }
    }
  }

  public void print() {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        System.out.print(ju[i][j] + "  ");
      }
      System.out.println();
    }
  }

  public static void main(String[] args) {
    Juzhen zu = new Juzhen();
    n = zu.input();
    zu.make();
    zu.print();
  }

  public int input() {
    try {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("输入你的螺旋矩阵的n:");
      String s = br.readLine();
      n = Integer.parseInt(s);
    }
    catch (IOException e) {}
    return n;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -