package com.lf.trianglenumber; public class Test { public static void main(String[] args) { // 打印的行数 int count = 10; // 创建二维数组 int[][] triangleNum = new int[count][]; // 遍历,创建一维数组并赋值 for (int i = 0; i < triangleNum.length; i++) { //创建一维数组 triangleNum[i] = new int[i+1]; // 遍历,给一维数组赋值 for (int j = 0; j < triangleNum[i].length; j++) { //判断是否是首位和末位,如果是就赋值1,不是就用上一行相邻的数相加 if (j==0 || j==triangleNum[i].length-1) { triangleNum[i][j] = 1; }else { triangleNum[i][j] = triangleNum[i-1][j]+triangleNum[i-1][j-1]; } System.out.print(""+triangleNum[i][j]+" "); } System.out.println(""); } } }