我正在尝试将 Pascal Triangle 打印到一百行,但 Java 的 int 似乎返回负值。我正在尝试使用 BigInteger,但每当我添加两个数字时,我都会得到 NullPointerException!我想我初始化了它们。这是我的代码:
import java.math.BigInteger;
public class Pascal {
public static void main(String[] args) {
BigInteger[][] p = new BigInteger[100][];
p[0] = new BigInteger[3];
p[0][1] = BigInteger.ONE;
for (int i = 1; i <= N; i++) {
p[i] = new BigInteger[i + 3];
for(int j = 0; j < p[i].length; j++){
p[i][j] = new BigInteger("0");
}
for (int j = 1; j < p[i].length - 1; j++)
p[i][j] = p[i-1][j-1].add(p[i-1][j]); //NPE!
}
for (int i = 0; i <= N; i++) {
for (int j = 1; j < p[i].length - 1; j++) {
System.out.print(p[i][j] + " ");
}
System.out.println();
}
}
}