# Using java to realize turning number

2022-06-24 10:01:59Klix

Enter a number k, Get one k*k Two dimensional array of , Realize the effect of turning numbers

The effect is as shown in the picture ：

Be careful ： Odd numbers and even numbers are different , The largest odd number is in the middle , Even numbers are not

The code is as follows ：

``````package  Circle number ;
​
import java.util.Scanner;
​
public class kkk {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt(), n = 0;
int[][] arryk = new int[k][k];
int t = k,h = 0,l = -1;
while (true) {
// Go to the right
while (true) {
n++;
l++;
arryk[h][l] = n;
​
if (l == t-1) {
break;
}
}
// End of interpretation
if (n == k * k) {
break;
}
// Go down
while (true) {
n++;
h++;
arryk[h][l] = n;
​
if (h == t-1) {
break;
}
​
}
// Go to the left
while (true) {
n++;
l--;
arryk[h][l] = n;
​
if (l == k - t) {
break;
}
}
if (n == k * k) {
break;
}
// Go up
while (true) {
n++;
h--;
arryk[h][l] = n;
if (h == k - t+1 ) {
break;
}
}
t--;
​
}
scanner.close();
for (int i = 0; i < k; i++) {
for (int p = 0; p < k; p++) {
System.out.printf("%-4d",arryk[i][p]);
}
System.out.println();
}
}
}``````