package xuexi;
public class YangHui {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum[][] = new int[8][];
for(int i = 0; i <= sum.length; i++){
sum[i] = new int[i+1];
for(int j = 0; j <= sum[i].length - 1; j++){
if(i == 0|| j ==0|| j == sum[i].length - 1){
sum[i][j] = 1;
}else{
sum[i][j] = sum[i-1][j] + sum[i-1][j-1];
}
System.out.print(sum[i][j] + "\t");
}
System.out.println();
}
}
}这是输出结果: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 1 7 21 35 35 21 7 1 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at xuexi.YangHui.main(YangHui.java:9)需要帮助的问题:我想知道为什么会提示下标越界?还有这一段代码我看的不是太明白,请求指教。if(i == 0|| j ==0|| j == sum[i].length - 1){ sum[i][j] = 1; }else{ sum[i][j] = sum[i-1][j] + sum[i-1][j-1]; }非常感谢!
添加回答
举报
0/150
提交
取消