請問 這樣寫錯在哪里?謝謝!
package com.Java;
import java.util.Arrays;
public class HelloWorld {
?
?public static void main(String[] args) {
??
??int[] scores = {89 , -23 , 64 , 91 , 119 , 52 , 73};
??
??System.out.println("成績前三名為:");
??
??HelloWorld hello = new HelloWorld();
??
??hello.sortAndPrint(scores);
???
?}
?
?public void sortAndPrint(int[] scores){
??
?Arrays.sort(scores);
?
?int num=0;
?
?for (int i=scores.length ;i>=0;i--){
??
??if (scores[i]>0 && scores[i]<=100){
???
???System.out.println(scores[i]);
????
???num++;
???
???} ?else?{
???
???continue;
???
???}
??
??if (num==3){break;}
??
??}
??
?}
?
?
}
運行結(jié)果
成績前三名為:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
?at com.Java.HelloWorld.sortAndPrint(HelloWorld.java:27)
?at com.Java.HelloWorld.main(HelloWorld.java:15)
2015-07-03
下標(biāo)越界
2015-06-29
length-1
2015-06-29
找到錯誤了,當(dāng)數(shù)組有7個數(shù)的時候,scores.length=7,而數(shù)組中最高分的 數(shù)是 scores[6],
所以 int i=scores.length-1