class Demo4{
public static void main(String[] args) {
int x = 5;
int[][]arr = new int[x][x];
if(x%2==1){
arr[x/2][x/2]=x*x;
}
int y = 0;
for (int i=0;i<3 ;i++ )
{
for (int j=i;j<x-1-i ;j++ )
{
arr[i][j]=++y;
}
for (int j=i;j<x-1-i ;j++ )
{
arr[j][x-1-i]=++y;
}
for (int j=x-1-i;j>i;j-- )
{
arr[x-1-i][j]=++y;
}
for(int j=x-1-i;j>i;j--)
arr[j][i]=++y;
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < x; j++)
System.out.print(arr[i][j] + "\t");
System.out.println("");
}
}
}class Demo4{
public static void main(String[] args) {
int x = 5;
int[][]arr = new int[x][x];
if(x%2==1){
arr[x/2][x/2]=x*x;
}
int y = 0;
for (int i=0;i<3 ;i++ )
{
for (int j=i;j<x-1-i ;j++ )
{
arr[i][j]=++y;
}
for (int j=i;j<x-1-i ;j++ )
{
arr[j][x-1-i]=++y;
}
for (int j=x-1-i;j>i;j-- )
{
arr[x-1-i][j]=++y;
}
for(int j=x-1-i;j>i;j--)
arr[j][i]=++y;
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < x; j++)
System.out.print(arr[i][j] + "\t");
System.out.println("");
}
}
}class Demo4{
public static void main(String[] args) {
int x = 5;
int[][]arr = new int[x][x];
}
int y = 0;
for (int i=0;i<3 ;i++ )
{
for (int j=i;j<x-1-i ;j++ )
{
arr[i][j]=++y;
}
for (int j=i;j<x-1-i ;j++ )
{
arr[j][x-1-i]=++y;
}
for (int j=x-1-i;j>i;j-- )
{
arr[x-1-i][j]=++y;
}
for(int j=x-1-i;j>i;j--)
arr[j][i]=++y;
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < x; j++)
System.out.print(arr[i][j] + "\t");
System.out.println("");
}
}
}
}
} |
|