java 输出图形
1.效果图
代码:
package com.zhanggaosong;
public class Main {
public static void main(String[] args) {
final int size = 5;
int[][] ary = new int[size][size];
int num = 1;
for(int row = 0; row < ary.length; row++){
for(int i = row, col = 0; i >= 0; i--){
ary[i][col++] = num++;
}
}
// for 输出
for(int i=0;i<ary.length;i++){
for(int j=0;j<ary[i].length;j++){
if(ary[i][j]!=0){
System.out.print(ary[i][j]+" ");
}
}
System.out.println();
}
// foreach 输出
for(int[] aryItem : ary){
for(int value: aryItem){
if(value != 0){
System.out.print(value + " ");
}
}
System.out.println();
}
}
}