我求的是这个杨辉三角:1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 不知道哪错了package pck;public class whileloop{
public static void main(String [] args){
int i,j;
int a[][]=new int[5][5];
for(i=0;i<5;i++){
for(j=0;j<5;j++){
a[i][j]=0;
}
}
for( i=0;i<5;i++){
a[i][0] = 1;
for( j=0;j<5;j++){
if(j==i){
a[i][j]=1;
}
}
}
for( i=0;i<5;i++){
for( j=0;j<5;j++){
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for( i=0;i<5;i++){
for( j=0;j<=i;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
提示的错误:java.lang.ArrayIndexOutOfBoundsException: -1 at pck.whileloop.main(whileloop.java:22)Exception in thread "main"
1 1
1 2 1
1 3 3 1
1 4 6 4 1 不知道哪错了package pck;public class whileloop{
public static void main(String [] args){
int i,j;
int a[][]=new int[5][5];
for(i=0;i<5;i++){
for(j=0;j<5;j++){
a[i][j]=0;
}
}
for( i=0;i<5;i++){
a[i][0] = 1;
for( j=0;j<5;j++){
if(j==i){
a[i][j]=1;
}
}
}
for( i=0;i<5;i++){
for( j=0;j<5;j++){
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for( i=0;i<5;i++){
for( j=0;j<=i;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
提示的错误:java.lang.ArrayIndexOutOfBoundsException: -1 at pck.whileloop.main(whileloop.java:22)Exception in thread "main"
解决方案 »
- 取代new sun.misc.BASE64Encoder().encode的新的API是什么?TKS
- 有个关于byte的问题
- 关于对象流的问题
- 请问这是什么错误:Exception in thread "main" java.lang.NoClassDefFoundError
- 如何在JEDITORPANE中显示文本和图片(混合)!!!!!!!!!!!!!!!!!!!
- 为何不进入for
- JBuilder插件最新版(2003-2-18制作),插件源程序等,欢迎下载 :)
- 怎样判断resultset中有多少条记录?
- 什么时候应该用static方法,欢迎大家讨论
- 关于applet使用list控件的问题,会者不难
- JApplet中的jTextArea为什么不能显示,请帮忙
- 反编译之后出现goto 以及e;,怎样变成可重新编译的代码?
for( j=0;j<5;j++){
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
22行附近
当i=0,j=0时,a[i-1][j-1]越界了
for (i = 1; i < 5; i++) {
for (j = 1; j < 5; j++) {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}