我的程序给了我错误ArrayIndexOutOfBoundsException

时间:2017-02-28 18:53:18

标签: java

  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
  at newpackage.NewClass5.main(NewClass5.java:33)
  at if(average[k]>second) and i dont know why its giving me that

我的代码给了我想要查找的错误并删除每行的最小值和最大值,然后计算每一行的平均值并使查找成为赢家

肖恩 8.4 5.6 7.2 9.2 8.1 5.7

乔治 5.4 8.7 6 9.9 7.6 8.3

金 7.5 5.2 5.9 9.1 8.5 9

  Scanner input=new Scanner(System.in);
  System.out.println("Enter the number of Swimmers");
  int n = input.nextInt();
  String[] name = new String[n];
  System.out.println("Enter the number of Juries");
  int m = input.nextInt();
  double[] jurie=new double[m];
  double[] average=new double[n];
  for(int i=0;i<n;i++){
  name[i] = input.next();
  for(int pidh=0;pidh<m;pidh++){
  jurie[i]=input.nextDouble();
  double total = 0.0;
  double max1 = jurie[pidh];
  double min = jurie[pidh];
  for(int q=0;q<m;q++){
  if(jurie[pidh] > max1){
                max1 = jurie[pidh];}
        if(jurie[pidh] < min){
           min = jurie[pidh];}
        total=total + jurie[i];
       }
        average[i]=(total-(min+max1))/(m-2);
        }

    int pos=0;
    double second = average[0];
       for(int k=0;k<m;k++){
                 if(average[k]>second)
        {
            second = average[k];
            pos = k;
        }
    }
    System.out.println("\n" +name[pos] + " is the winner with " + second +  " points.");  
  }
   }
   }

2 个答案:

答案 0 :(得分:0)

K取决于m,平均值是n。如果m大于n,你会得到异常。尝试清理代码。很难读懂。

还在违规行中放置一个断点。然后检查值。

答案 1 :(得分:0)

你应该改变:

for(int k=0;k<m;k++){

为:

for(int k=0;k<n;k++){

因为n包含您为其计算平均值的游泳者数量(根据代码double[] average=new double[n];)。