- /*
-
- 实现螺旋赋值
-
- 1 2 3 4 5
- 6 7 8 9 10
- 11 12 13 14 15
- 16 17 18 19 20
- 21 22 23 24 25
- || ||
- \/ \/
- 1 2 3 4 5
- 16 17 18 19 6
- 15 24 25 20 7
- 14 23 22 21 8
- 13 12 11 10 9
- *
- *
- * */
- package com.test;
- public class LuoXuan {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println("请输入一个小于或等于10的整数:");
- int c = new java.util.Scanner(System.in).nextInt();
- int arr[][] = new int[c][c];
- int sum = 1;
- for (int j = 0; j < c / 2 + 1; j++) {
-
- //输出第一行
- for (int i = j; i < arr.length - j; i++) {
- arr[j][i] = sum++;
- }
- //输出最后一列
- for (int i = j + 1; i < arr.length - j; i++) {
- arr[i][arr.length - 1 - j] = sum++;
- }
- //输出最后一行
- for (int i = arr.length - 2 - j; i >= j; i--) {
- arr[arr.length - 1 - j][i] = sum++;
- }
- //输出第一列
- for (int i = arr.length - 2 - j; i > j; i--) {
- arr[i][j] = sum++;
- }
- }
- printArr(arr);
- }
- //打印数组
- public static void printArr(int arr[][]) {
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[i].length; j++) {
- System.out.print(arr[i][j] + "\t");
- }
- System.out.println();
- }
- }
- }
复制代码
|