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”是位置数组中最后一个数组的大小。但并非所有人都具有相同的大小。因此,如果较早的数组较小(例如,如果您输入 1、2、3),则较早的数组会超出范围。您必须将 pos1 循环到 position[index1].length (因此将第 73 行更改为for(int pos1= 0; pos1 < positions[index1].length; pos1++)

于 2013-11-04T03:02:12.830 回答