求大神指錯(cuò)
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? HelloWorld hello =new HelloWorld();
? ? ? ? int[] scores={89,-23,64,91,119,52,73};
? ? ? ? System.out.println("前三名成績(jī):");
? ? ? ? hello.print(scores);
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ??
? ??
? ??
? ? public void print(int [] score){
? ? ? ? Arrays.sort(score);
? ? ? ? int count=0;
? ? ? ? for(int i=score.length-1;i>=0;i++){
? ? ? ? ? ??
? ? ? ? ? ? if(score[i]>=0&&score[i]<=100){
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? System.out.println(score[i]);
? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? }
? ? ? ? ? ? else{?
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? if(count>3){
? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? } ??
? ? }
? ??
? ??
? ??
? ??
? ??
? ??
}
錯(cuò)誤是這樣的:
前三名成績(jī):
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at HelloWorld.print(HelloWorld.java:23)
at HelloWorld.main(HelloWorld.java:9)
2015-10-09
倒敘遍歷score i應(yīng)該是i--吧