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();
}
}
}這是輸出結(jié)果: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)需要幫助的問題:我想知道為什么會(huì)提示下標(biāo)越界?還有這一段代碼我看的不是太明白,請(qǐng)求指教。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]; }非常感謝!
添加回答
舉報(bào)
0/150
提交
取消