感覺沒毛病啊。。
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.ranklist(scores);
???
??? //定義方法完成成績排序并輸出前三名的功能
}
??? public void ranklist(int[] scores){
??????? Arrays.sort(scores);
??????? int num=0;
??????? for(int i=scores.length;i>=0;i--){
????????
???????? if(scores[i]<0||scores[i]>100){continue;
??????? }
?????????? num++;
???????????
??????? if(num>3){break;}
????? System.out.println(scores[i]);
??????? }
?????
}?
2019-12-15
?i=scores.length這個(gè)代碼會導(dǎo)致數(shù)組訪問越界。舉個(gè)案例:int[] scores=new int[2]; ?由于數(shù)組起始是從0開始,到1就結(jié)束了, 現(xiàn)在初始化值scores[2]=1; 編譯時(shí)提示數(shù)組越界。你這個(gè)代碼存在的問題就是數(shù)組訪問越界了 ?
2019-12-08
i=scores.length-1,因?yàn)閿?shù)組是從scores[0]開始,i=scores.length大于了數(shù)組長度。好像還少了個(gè)}