The idea is to create an integer two-dimensional array containing 10 one-dimensional arrays. Using a double-layer loop, initialize the size of each second layer array in the outer loop. In the inner loop, first assign the array elements on both sides to 1, and the other values are calculated by formulas, and then the array elements are output.
The code copy is as follows:
public class YanghuiTriangle {
public static void main(String[] args) {
int triangle[][]=new int[10][];// Create a two-dimensional array
// traverse the first layer of a two-dimensional array
for (int i = 0; i < triangle.length; i++) {
triangle[i]=new int[i+1];// Initialize the size of the second layer array
// traverse the second layer array
for(int j=0;j<=i;j++){
// Assign the array elements on both sides to 1
if(i==0||j==0||j==i){
triangle[i][j]=1;
}else{// Other values are calculated by formula
triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];
}
System.out.print(triangle[i][j]+"/t"); // Output array element
}
System.out.println(); //Win
}
}
}