之前写过一篇利用*打印三角形汇总,网友需要查看可以去本专栏查找之前的文章,这里利用二维数组+嵌套循环打印“数字”三角形,汇总如下,话不多说,直接上代码:
/*** 打印如下数字三角形图形*/
public class Work {public static void main(String[] args) {int num = 5;/*** 打印如下图形:* 1* 22* 333* 4444* 55555*/int[][] arr1 = new int[num][];for (int i = 0; i <= arr1.length - 1; i++) {arr1[i] = new int[i + 1];for (int j = 0; j <= arr1[i].length - 1; j++) {arr1[i][j] = i + 1;System.out.print(arr1[i][j]);}System.out.println();}System.out.println();/*** 打印如下的效果* 1* 22* 333* 4444* 55555*/int[][] arr2 = new int[num][];for (int i = 0; i <= arr2.length - 1; i++) {arr2[i] = new int[i + 1];for (int j = 0; j < num - i - 1; j++) {System.out.print(" ");}for (int j = 0; j <= arr2[i].length - 1; j++) {arr2[i][j] = i + 1;System.out.print(arr2[i][j]);}System.out.println();}System.out.println();/*** 打印如下的效果* 11111* 2222* 333* 44* 5*/int[][] arr3 = new int[num][];for (int i = 0; i <= arr3.length - 1; i++) {arr3[i] = new int[num - i];for (int j = 0; j <= arr3[i].length - 1; j++) {arr3[i][j] = i + 1;System.out.print(arr3[i][j]);}System.out.println();}System.out.println();/*** 打印如下的效果* 11111* 2222* 333* 44* 5*/int[][] arr4 = new int[num][];for (int i = 0; i <= arr4.length - 1; i++) {arr4[i] = new int[num - i];for (int j = 0; j <= i - 1; j++) {System.out.print(" ");}for (int j = 0; j <= arr4[i].length - 1; j++) {arr4[i][j] = i + 1;System.out.print(arr4[i][j]);}System.out.println();}}
}
idea中运行效果如下: