1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16
望各位仁兄帮帮忙。
import java.io.*;
public class snake
{
public static void main(String arg[])
{
int max=10;
int num=0;
String s;
int d,m,i,j,N;
int a,b;
s = Input();
num = str2int(s);
N = num;
int A[][] = new int[N][N];
m = 1;
i = 1;
j = 1;
d = 1;
if( N>=1&&N<=max )
{
while( m <= N*N )
{
A[i-1][j-1] = m;
switch ( d )
{
case 1:
i = i + 1;
if( j==1 )
d = 2;
else
d = 4;
break;
case 2:
i = i - 1;
j = j + 1;
if( j == N)
d = 1;
else if( i == 1 )
d = 3;
break;
case 3:
j = j + 1;
if( i == N )
d = 2;
else
d = 4;
break;
case 4:
i = i + 1;
j = j - 1;
if( i == N )
d = 3;
else if( j == 1 )
d = 1;
break;
}
m++;
}
System.out.println("Entrecote by snake :");
for( a=0;a<N;a++)
{
for( b=0;b<N;b++)
{
if( A[a][b] < 10 )
System.out.print(A[a][b]+" ");
else
System.out.print(A[a][b]+" ");
}
System.out.println("");
}
}
else
System.out.println("Input N error! N should be 1-10!");
}
public static String Input()
{
String s = "";
System.out.print("please input a num(1-10) :");
try
{
BufferedReader str = new BufferedReader(new InputStreamReader(System.in));
s = str.readLine();
}
catch(IOException e){};
System.out.println("You have entered : "+ s );
return s;
}
public static int str2int(String s)
{
int num = 0;
num = Integer.parseInt(s);
return num;
}
}