代码有解释:
import java.util.Scanner;
public class YangHuiText {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要打印行数:");
int n = input.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
arr[i][0] = 1;
arr[i][j] = 1;
}
}
for (int i = 2; i < arr.length; i++) {
for (int j = 1; j < i; j++) {
arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < i+1; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-1800.html