排列顺序不对怎么调
试过两个FOR循环从大到小开始,但是连输出也没有,报错
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at score.chongzai.scores(chongzai.java:28)
at score.chongzai.main(chongzai.java:12
试过两个FOR循环从大到小开始,但是连输出也没有,报错
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at score.chongzai.scores(chongzai.java:28)
at score.chongzai.main(chongzai.java:12
2017-07-13
public class HelloWorld {
//完成 main 方法
public static void main(String[] args) {
HelloWorld hello = new HelloWorld();
System.out.println("成绩前三:");
hello.PaiMing();
}
//定义方法完成成绩排序并输出前三名的功能
public void PaiMing(){
int[] scores={89,-23,64,91,119,52,73};
//int Thirdscore=scores[0];
for(int i=0;i<scores.length-1;i++){
for(int j=0;j<scores.length-i-1;j++){
if(scores[j]>scores[j+1]){
int temp=scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp;
}
}
}
for(int i=scores.length-1;i>2;i--){
if(scores[i]<0||scores[i]>100){
continue;
}
System.out.println(scores[i]);
}
}
}
/*这是我的代码,参考下吧。*/
举报