The idea is to create an integer two-dimensional array containing 10 one-dimensional arrays. Uses a two-tier loop to initialize the size of each second-tier array in the outer loop. In the inner loop, you assign the array elements on either side to 1, the other values are calculated by the formula, and then the array elements are output.
Copy Code code as follows:
public class Yanghuitriangle {
public static void Main (string[] args) {
int triangle[][]=new int[10][];//Create a two-dimensional array
Traversing the first layer of a two-dimensional array
for (int i = 0; i < triangle.length; i++) {
Triangle[i]=new int[i+1];//Initializes the size of the second-tier array
Traversing the second-tier array
for (int j=0;j<=i;j++) {
Assign the array elements on either side to 1
if (i==0| | j==0| | J==i) {
Triangle[i][j]=1;
}else{//other numerical 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 (); Line Wrap
}
}
}