int n = Integer.parseInt(s); int m = 0; int k = 0; int[][] a = new int[n][n]; if (n % 2 == 0) { m = n; } else { m = n / 2 + 1; }
for (int i = 0; i < m; i++) { for (int j = i; j < n - i; j++) { k++; a[i][j] = k; } for (int j = i + 1; j < n - i; j++) { k++; a[j][n - 1 - i] = k; } for (int j = n - i - 2; j >= i; j--) { k++; a[n - i - 1][j] = k; } for (int j = n - i - 2; j >= i + 1; j--) { k++; a[j][i] = k; } }
for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { if (a[i][j] < 10) { System.out.print(" "); } System.out.print(a[i][j] + " "); } System.out.println(); } System.out.println();
} }
自己写了一个不知道你说的是不是这个意思 package xf.suanfa; public class CirclePrint { private int length; private int[][] data; int current_num; //right=1;down=2;left=3;up=4; int lastDirect; int width;//矩阵宽度 int i=0,j=-1;//i代表行号,j代表列号 static final int GORIGHT=1; static final int GODOWN=2; static final int GOLEFT=3; static final int GOUP=4; public CirclePrint(int length) { setLength(length); } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public void init() { current_num=1; lastDirect=GORIGHT; int width1=(int)Math.sqrt(length); double width2=Math.sqrt(length); if((width2-width1)==0 ) { width=width1; } else { width=width1+1; } data=new int[width][width]; } public void doJob() {
public static void main(String[] args) {
String s = JOptionPane.showInputDialog("输入矩阵");
int n = Integer.parseInt(s);
int m = 0;
int k = 0;
int[][] a = new int[n][n]; if (n % 2 == 0) {
m = n;
} else {
m = n / 2 + 1;
}
for (int i = 0; i < m; i++) {
for (int j = i; j < n - i; j++) {
k++;
a[i][j] = k;
}
for (int j = i + 1; j < n - i; j++) {
k++;
a[j][n - 1 - i] = k;
}
for (int j = n - i - 2; j >= i; j--) {
k++;
a[n - i - 1][j] = k;
}
for (int j = n - i - 2; j >= i + 1; j--) {
k++;
a[j][i] = k;
} }
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i][j] < 10) {
System.out.print(" ");
} System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
}
package xf.suanfa;
public class CirclePrint {
private int length;
private int[][] data;
int current_num;
//right=1;down=2;left=3;up=4;
int lastDirect;
int width;//矩阵宽度
int i=0,j=-1;//i代表行号,j代表列号
static final int GORIGHT=1;
static final int GODOWN=2;
static final int GOLEFT=3;
static final int GOUP=4;
public CirclePrint(int length)
{
setLength(length);
}
public int getLength() {
return length;
} public void setLength(int length) {
this.length = length;
}
public void init()
{
current_num=1;
lastDirect=GORIGHT;
int width1=(int)Math.sqrt(length);
double width2=Math.sqrt(length);
if((width2-width1)==0 )
{
width=width1;
}
else
{
width=width1+1;
}
data=new int[width][width];
}
public void doJob()
{
for(;current_num<length+1;current_num++)
{
if(canGoLastDirect())
goLastDirect();
else
goNewDirect();
}
}
private boolean canGoLastDirect()
{
if(lastDirect==GORIGHT)
return canGoRight();
else if(lastDirect==GODOWN)
return canGoDown();
else if(lastDirect==GOLEFT)
return canGoLeft();
else
return canGoUp();
}
private void goLastDirect()
{
if(lastDirect==GORIGHT)
goRight();
else if(lastDirect==GODOWN)
goDown();
else if(lastDirect==GOLEFT)
goLeft();
else
goUp();
}
private void goNewDirect()
{
lastDirect=(lastDirect+1)%4;
goLastDirect();
}
private boolean canGoRight()
{
int copyJ=j;
copyJ++;
if(copyJ>=width||data[i][copyJ]!=0)
return false;
return true;
}
private void goRight()
{
j++;
data[i][j]=current_num;
}
private boolean canGoDown()
{
int copyI=i;
copyI++;
if(copyI>=width||data[copyI][j]!=0)
return false;
return true;
}
private void goDown()
{
i++;
data[i][j]=current_num;
}
private boolean canGoLeft()
{
int copyJ=j;
copyJ--;
if(copyJ<0||data[i][copyJ]!=0)
return false;
return true;
}
private void goLeft()
{
j--;
data[i][j]=current_num;
}
private boolean canGoUp()
{
int copyI=i;
copyI--;
if(copyI<0||data[copyI][j]!=0)
return false;
return true;
}
private void goUp()
{
i--;
data[i][j]=current_num;
}
public void print()
{
init();
doJob();
for(int i=0;i<width;i++)
{
for(int j=0;j<width;j++)
System.out.print(data[i][j]+" ");
System.out.println("");
}
}
public static void main(String[] args) { CirclePrint cp=new CirclePrint(25);
cp.print();
}}