幫忙看下為什么錯了?謝謝
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};
????? hello.work(scores);
??? }
?? ?
??? //定義方法完成成績排序并輸出前三名的功能
??? public void work(int[] scores){
??? int count=0;
??? Arrays.sort(scores);
??? for(int i=0;i<scores.length-1;i++){
??????? if(scores[scores.length-i]>100||scores[scores.length-i]<0){
??????????? continue;
??????????? }
??????? count=count+1;
??????? System.out.println(scores[i]);
??????? if(count>2){
??????????? break;
??????? }
??????????? ?
??? }
???? ?
?? ?
}
}
2015-04-22
數組下標越界異常 錯誤代碼是這行? if(scores[scores.length-i]>100||scores[scores.length-i]<0) 當i=0的時候就數組下標越界了是不是 還有盡然你要輸出前三數字 你已近使用了Arrays工具類 那就是已經按照從小到大排好序的 直接輸出最后三個不就行了嗎?