1 package tuxing; 2 3 4 //平面图形题(二维数组) 5 6 7 public class tuxing1 { 8 public static void main(String[] args) 9 { 10 11 12 int arr[][]=new int[3][9]; 13 int x=2; 14 int y=0; 15 boolean order=false; 16 for(int i=1;i<=9;i++) 17 { 18 arr[x][y]=i; 19 y++; 20 if(!order) 21 { 22 x--; 23 } 24 if(order) 25 { 26 x++; 27 } 28 if(x<0) 29 { 30 order=true; 31 x=x+2; 32 } 33 if(x>2) 34 { 35 order=false; 36 x=x-2; 37 } 38 } 39 for(int i=0;i<arr.length;i++) 40 { 41 for(int j=0;j<arr[i].length;j++) 42 { 43 if(arr[i][j]==0) 44 { 45 System.out.print(" "); 46 } 47 else 48 { 49 System.out.print(arr[i][j]); 50 51 } 52 } 53 System.out.println(); 54 55 } 56 57 58 } 59 60 }
1 package tuxing; 2 3 4 //平面图形题(二维数组) 可变长度!!!! 5 6 7 public class tuxing1 { 8 public static void main(String[] args) 9 { 10 int num=40; 11 int hight=num/4+1; 12 int weight=num; 13 int arr[][]=new int[hight][weight]; 14 int x=hight-1; 15 int y=0; 16 boolean order=false; 17 for(int i=1;i<=num;i++) 18 { 19 arr[x][y]=i; 20 y++; 21 if(!order) 22 { 23 x--; 24 } 25 if(order) 26 { 27 x++; 28 } 29 if(x<0) 30 { 31 order=true; 32 x=x+2; 33 } 34 if(x>hight-1) 35 { 36 order=false; 37 x=x-2; 38 } 39 } 40 for(int i=0;i<arr.length;i++) 41 { 42 for(int j=0;j<arr[i].length;j++) 43 { 44 if(arr[i][j]==0) 45 { 46 System.out.print(" "); 47 } 48 else 49 { 50 if(x<10) 51 System.out.print(arr[i][j]+" "); 52 else 53 System.out.print(arr[i][j]); 54 55 } 56 } 57 System.out.println(); 58 59 } 60 61 62 } 63 64 }
时间: 2024-11-06 09:43:35