杨辉三角:
分析:
1.使用二维数组,但是先设置二维数组的外层数组的长度,内层长度后面设置
2.使用第一层循环,用来遍历以及控制内层长度
3.使用第二层循环,用if语句来控制索引值为0和内存数组的最大索引,都打印1
4.杨辉三角特性,除了第一个值和最后一个值是1以外,其他的是上层索引值一致的值再加上上层索引值减1的值
代码实现:
public class Test {
public static void main(String[] args) {
int[][] arr = new int[10][];
for (int x = 0; x < arr.length; x++) {
arr[x] = new int[x + 1];
for (int j = 0; j < arr[x].length; j++) {
if (j == 0 | j == arr[x].length - 1) {
arr[x][j] = 1;
} else {
arr[x][j] = arr[x - 1][j] + arr[x - 1][j - 1];
}
System.out.print(arr[x][j] + "\t");
}
System.out.println();
}
}
}