2017-02-28 1 views
0
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 

내 코드는 나에게 내가 찾은 다음 각각의 평균을 계산 각 라인의 최소 및 최대를 제거하고 만들고 싶어 그 오류를주는 찾을 나에게 승자 오류는 ArrayIndexOutOfBoundsException를 제공내 프로그램은

숀 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."); 
    } 
    } 
    } 

답변

0

당신은 변경해야합니다 :

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

에 : n

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

당신이 (코드 double[] average=new double[n]; 당) 평균값을 계산하고있는 선수의 수를 보유하고 있습니다.

+0

이제 효과가있었습니다. 고마워, 누군가가 여전히이 스레드를 본다면. 인쇄하는 값은 10 개 이상이지만 올바른 숫자를 평균화하려면 8.55, 12.45, 13.5가 표시됩니다. – Karoqi

+0

@ Karoqi You 're welcome :) – AntonH

0

K는 m에 의존하고 평균은 크기 n이다. m이 n보다 크면 예외가 발생합니다. 코드를 정리하십시오. 읽을 수 없습니다.

또한 위반하는 줄에 중단 점을 배치하십시오. 그런 다음 값을 검사하십시오.

관련 문제