杨辉三角(java实现)

时间:2022-06-28 15:03:41
 package lizi;
import java.util.Scanner;
public class Yanghui {
private static Scanner input;
public static void main(String[] args) {
test_YH();
}
public static void test_YH() {
System.out.print("请输入行数:");
input = new Scanner(System.in);
int row = input.nextInt();
int col = row;
int[][] array = new int[row][col]; for (int i = 1; i < row; i++) {
for (int j = row; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
if (i == j || j == 0) {
array[i][j] = 1;
} else {
array[i][j] = array[i - 1][j - 1] + array[i - 1][j];
}
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}