上代码
import java.util.Scanner;
public class 杨辉三角 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入杨辉三角层数");
int m = scanner.nextInt();
int array[][] = new int[m][];
for (int i = 0; i < array.length; i++) {//一维数组开空间
array[i] = new int[i+1];
}
for (int i = 0; i < array.length; i++) {//首尾全部赋值成1
array[i][0] = 1;
array[i][i] = 1;
}
for (int i = 0; i < array.length; i++) {//内层赋值
for (int j = 0; j < array.length; j++) {
if(j > 0 && j < i) {//内层赋值范围
array[i][j] = array[i - 1][j] + array[i - 1][j - 1];//内层规律
}
}
}
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}
代码已分块做注释
:)