我自己写了点,今天没事发个帖子,求指教
import java.util.Scanner;
class Night
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int Num=13;
//确保输入的数值小于10大于0
while (Num>10|Num<0)
{
System.out.println("请输入小于10的数:");
Num = sc.nextInt();
}
int a[][] =new int [Num][Num];
int b=-1,k=0,i=0,j=0,c=1;
//j = 0 i值变化,由a[0][0]直到加到a[i][0],并赋值
for (int y=0;y<a.length-1 ;y++ )
{
a[i][j]=++k;i+=(-b);
}
//循环a.length-1次,每次少一步
for (int x = a.length-1;x>0;x-- )
{
b=-b;
// 每次:a[i][j],i值不变,j值或加x次1,或减x次1,并赋值
for (int y=0;y<x;y++ )
{
a[i][j]=++k;j+=b;
}
c=-c;
// 每次:a[i][j],j值不变,i值或加x次1,或减x次1,并赋值
for (int y=0;y<x ;y++ )
{
a[i][j]=++k;i+=c;
}
}
a[i][j]=++k;
System.out.println("======================================================");
for (int x=0;x<a.length;x++ )
{
for (int y =0;y<a.length;y++ )
{
System.out.print(a[y][x]+"\t");
}
System.out.println();
}
}
}
/*
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
*/ |
|