帮我看下这个程序有什么错误,
说是数组越界
class pass{
 int i,j,k=0,c,d,size=3;
 int a[][]=new int[size][size];
 int n=size;
 void xunhuan(){
    for(j=0,i=0;i<=n;c=i++){
      a[c][j]=++k;
      System.out.println(+k);
      if(i==0&&j==0)
      { 
      for(j=0;j<=n;j++){
      a[c][j]=d++;
      System.out.print(+d);}
      }
       else{
       for(j=0;j<=n;j++)
       {a[c+1][j]=a[c][j-1]+a[c][j];
       System.out.print(+a[c+1][j]);} 
       }
      }
    }
public static void main(String args[]){
 pass b=new pass();
 b.xunhuan();
  }
 }
要求输出结果是:
1 2 3
2 3 5
3 5 8

解决方案 »

  1.   

    这位同学,我不知道你的程序目的是什么,但是你注意你的for循环都是for(j=0;j<=n;j++)或者for(i=0;i<=n;i++)
    如果你的n是3,for循环会走4次的,你是3×3的数组,走到第四次不越界才怪呢
      

  2.   

    class   pass{
      int   i,j,k=0,c,d,size=4;
      int   a[][]=new   int[size][size];
      int   n=size;
      void   xunhuan()
      {
       for ( i = 0,j=0; i < n; i++ )
       {
    for ( j = 0; j < n; j ++ )
    {
    if ( i == 0 )
    {
    if ( j ==0 )
    {
    a[i][j] = 1;
    }
    else 
    {
    int j1 = (j - 1) > 0 ? (j-1) : 0;
    int j2 = (j - 2) > 0 ? (j-2) : 0;
    a[i][j]=a[i][j1] + a[i][j2];
    }
    }
    else  //i>0
    {
    if ( ( j+1 ) < n )
    {
    a[i][j] = a[i-1][j+1];
    }
    else
    {
    int j1 = (j - 1) > 0 ? (j-1) : 0;
    int j2 = (j - 2) > 0 ? (j-2) : 0;
    a[i][j]=a[i][j1] + a[i][j2];
    }
    }
    }
       }
    for ( i = 0; i < n; i++ )
    {
    for ( j =0; j < n; j++ )
    System.out.print(" " + a[i][j]);
    System.out.println("");
    }
      }
    public   static   void   main(String   args[]){
      pass   b=new   pass();
      b.xunhuan();
        }
      } 不知道是不是你要的结果
      

  3.   

    把上面的size=4改为size=3;这是我自已试size=4是不是我想要的值。
    size=3时,输出
    1 2 3
    2 3 5
    3 5 8
    size=4时输出
    1 2 3 5
    2 3 5 8
    3 5 8 13
    5 8 13 21
    size=5时输出
    1  2  3  5  8  13
    2  3  5  8  13 21 
    3  5  8  13 21 34
    5  8  13 21 34 55
    8  13 21 34 55 89  
      

  4.   

    下在是我优化过的代码
    class   pass1{
      int   i,j,k=0,c,d,size=3;
      int   a[][]=new   int[size][size];
      int   n=size;
      void   xunhuan()
      {
       for ( i = 0,j=0; i < n; i++ )
       {
    for ( j = 0; j < n; j ++ )
    {
    if ( i == 0 && j == 0 )
    {
    a[i][j] = 1;
    }
    else 
    {
    if ( (i-1) > 0  && ( j+1 ) < n )
    {
    a[i][j] = a[i-1][j+1];
    }
    else
    {
    int j1 = (j - 1) > 0 ? (j-1) : 0;
    int j2 = (j - 2) > 0 ? (j-2) : 0;
    a[i][j]=a[i][j1] + a[i][j2];
    }
    }
    }
       }
    for ( i = 0; i < n; i++ )
    {
    for ( j =0; j < n; j++ )
    System.out.print(" " + a[i][j]);
    System.out.println("");
    }
      }
    public   static   void   main(String   args[]){
      pass   b=new   pass();
      b.xunhuan();
        }
      }
      

  5.   

    上面的错了,晕死,应改为:
    class   pass{
      int   i,j,k=0,c,d,size=3;
      int   a[][]=new   int[size][size];
      int   n=size;
      void   xunhuan()
      {
       for ( i = 0,j=0; i < n; i++ )
       {
    for ( j = 0; j < n; j ++ )
    {
    if ( i == 0 && j == 0 )
    {
    a[i][j] = 1;
    }
    else 
    {
    if ( (i-1) >= 0  && ( j+1 ) < n )
    {
    a[i][j] = a[i-1][j+1];
    }
    else
    {
    int j1 = (j - 1) > 0 ? (j-1) : 0;
    int j2 = (j - 2) > 0 ? (j-2) : 0;
    a[i][j]=a[i][j1] + a[i][j2];
    }
    }
    }
       }
    for ( i = 0; i < n; i++ )
    {
    for ( j =0; j < n; j++ )
    System.out.print(" " + a[i][j]);
    System.out.println("");
    }
      }
    public   static   void   main(String   args[]){
      pass   b=new   pass();
      b.xunhuan();
        }
      } 
      

  6.   

    兄弟,代码格式要注意,看看我的方法class pass { 
      int i,j,k,c,d,size=5; 
      int a[][]=new int[size][size]; 
      int n=size; 
    //  void xunhuan() { 
    // for(j = 0,i = 0; i <= n; c = i++) { 
    //     a[c][j] = ++k; 
    //     System.out.println(k); 
    //     if(i == 0 && j == 0) {   
    //     for(j=0; j <= n; j++) { 
    //     a[c][j] = d++; 
    //     System.out.print(d);
    //     } 
    // } else{ 
    //       for(j = 0; j <= n; j++) {
    //        // if j = 0 a[c][j-1] = a[c][-1] break limit
    //       a[c+1][j] = a[c][j-1]+a[c][j]; 
    //       System.out.print(a[c+1][j]);}   
    //       } 
    //     } 
    //   } 
      void dataSequence() {
      //Max
      int a1 = 1, a2 = 2;
      if (this.size > 0) {
      int[] dataArray = new int[size * 2 -1];
      dataArray[0] = a1;
      dataArray[1] = a2;
      for (int i = 2; i < dataArray.length; i++) {
      dataArray[i] = dataArray[i-1] + dataArray[i-2];
      }
      // all data
      for (int j = 0; j < dataArray.length; j++) {
      System.out.print(dataArray[j] + " ");
      }
      System.out.println("next line");
      // out 
      for (int k = 0; k < this.size; k++) {
      for (int m = k; m < this.size + k; m++) {
      System.out.print(dataArray[m] + " ");
      }
      System.out.println("");
      }
      }
      }
       public   static   void   main(String   args[]) { 
      pass b = new pass(); 
      b.dataSequence(); 

    }