0

コードはここから始まります:

 System.out.print("Please enter number of rows: ");

  rows = keyboard.nextInt();  

      while(rows > MAX_ROWS || 0 > rows){
          System.out.print("ERROR: Out of range, try again: ");

          rows = keyboard.nextInt();


      }

      rowsTotal = new double[rows];
      positions = new double [rows][];

      for(index = 0; index < rowsTotal.length; index++){
          System.out.print("Please enter number of positions in row " + (char)(index+65) + " : ");
          pos = keyboard.nextInt();
          if(pos > MAX_POSITIONS){
              System.out.print("ERROR: Out of range, try again: ");
              pos = keyboard.nextInt();
          }
          positions[index] = new double[pos];


          }

      while(input != 'X'){    
      System.out.print("(A)dd, (R)emove, (P)rint,          e(X)it : ");

      input = keyboard.next().charAt(0);
      input = Character.toUpperCase(input);




      if(input == 'P'){
          for(int index1= 0; index1 < rowsTotal.length; index1++){
              System.out.print((char)(index1+65) + ":   " );

              for(int pos1= 0; pos1 < pos; pos1++){

                  **System.out.print(positions[index1][pos] + "  ");** 
              }
              System.out.print("[   " + totals + ", " + "   " + averages + "]");
              System.out.println();



      }

      }

以下のようなエラーメッセージ:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at xxxxx.main(xxxxx.java:75)

これはこのコード行ですSystem.out.print(positions[index1][pos] + " ");

4

1 に答える 1

1

コード全体を読めば、答えは簡単です。
a) 次のようにする必要がありますSystem.out.print(positions[index1][pos1] + " ");
b) 「pos」は、positions 配列内の LAST 配列のサイズです。しかし、それらのすべてが同じサイズであるとは限りません。したがって、以前の配列が小さい場合 (たとえば、1,2,3 を入力した場合)、以前の配列よりも範囲外になります。pos1 を position[index1].length にループする必要があります (そのため、73 行目を に変更しますfor(int pos1= 0; pos1 < positions[index1].length; pos1++)) 。

于 2013-11-04T03:02:12.830 に答える