The idea is to create an integer two-dimensional array containing 10 one-dimensional arrays. Using a double-level loop, initialize the size of each second-level array in the outer loop. In the inner loop, the array elements on both sides are first assigned the value 1, the other values are calculated through formulas, and then the array elements are output.
public class YanghuiTriangle { public static void main(String[] args) { int triangle[][]=new int[10][];// 创建二维数组 // 遍历二维数组的第一层 for (int i = 0; i < triangle.length; i++) { triangle[i]=new int[i+1];// 初始化第二层数组的大小 // 遍历第二层数组 for(int j=0;j<=i;j++){ // 将两侧的数组元素赋值为1 if(i==0||j==0||j==i){ triangle[i][j]=1; }else{// 其他数值通过公式计算 triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1]; } System.out.print(triangle[i][j]+"\t"); // 输出数组元素 } System.out.println(); //换行 } } }
For more java related articles using for loop to output Yang Hui triangle, please pay attention to the PHP Chinese website!