大佬求助?。槭裁次业?ldquo;求成績前三名”程序報輸出異常
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
?? ?at imooc.HelloWorld.sorts(HelloWorld.java:22)
?? ?at imooc.HelloWorld.main(HelloWorld.java:12)
下面是代碼段
package imooc;
import java.util.Arrays;
public class HelloWorld {? ?
??? //完成 main 方法
??? public static void main(String[] args) {
??????? int scores[]={89,-23,64,91,119,52,73};??????
??????? HelloWorld hello = new HelloWorld();
??????? hello.sorts(scores);
??? }
??? //定義方法完成成績排序并輸出前三名的功能?
???? public void sorts(int[] score){
???????? Arrays.sort(score);
???????? int[] a={0,0,0};
???????? int temp=0;
???????? for(int i=score.length;i>=0;i--){
???????????? if(score[i]>=0&&score[i]<=100&&temp<3){
???????????????? a[temp]=score[i];
???????????????? temp++;
???????????? }
???????? }
???????? System.out.println("考試成績前三名:");
???????? System.out.println(a[0]);
???????? System.out.println(a[1]);
???????? System.out.println(a[2]);
???? }??
}
2018-05-30
for循環(huán)那,int i=score.length應該為int i=score.length-1吧,要不然溢出了
2018-05-31
上面已經(jīng)解決,如果有萌新復制代碼請把代碼段的包名(package imooc;)去掉哦,那樣才能在慕課運行