import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int scores[] ={89,-23,64,91,119,52,73};
Arrays.sort(scores);
for(int i= scores.length ;i>=(scores.length-3);i--){
int a=0;
a++;
System.out.println("第"+a+"名"+" "+scores[i]);
}
}
}
循环第一次 i= scores.length,然后scores[i]
此时数组就越界了
你的意思是不是只输入排好序的前三名:
public class Hello {
public static void main(String[] args) {
int scores[] ={89,-23,64,91,119,52,73};
sort( scores);
}
static void sort(int scores[]){
int temp=0;
for (int i = 0; i < scores.length; i++) {
for (int j = 0; j < scores.length-1; j++) {
if(scores[j]<scores[j+1]){
temp = scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp;
}
}
}
for (int i = 0; i < scores.length-4; i++) {
System.out.println(scores[i]);
}
}
}
scores[i], i 是从0开始的! "scores.length"值为7,scores[i] ,i最大值为6!