按照下方样式,java实现三角形
// 杨辉三角形
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
package ArraySorted;
import java.util.Scanner;
public class ArrayTest03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行数:");
int n = scanner.nextInt();
int[][] array = new int[n][n];
// 把每一行的第一列和最后一列,设置为1
for (int i = 0; i < array.length; i++) {
array[i][0] = 1;
array[i][i] = 1;
}
// 设置中间元素
for (int i = 2; i < array.length; i++) {
for (int j = 1; j <= i; j++) {
array[i][j] = array[i-1][j-1] + array[i-1][j];
}
}
// 循环遍历二维数组
for (int i = 0; i < array.length; i++) {
for (int j = 0; j <=i; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}