排列順序不對怎么調
試過兩個FOR循環(huán)從大到小開始,但是連輸出也沒有,報錯
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
?? ?at score.chongzai.scores(chongzai.java:28)
?? ?at score.chongzai.main(chongzai.java:12
試過兩個FOR循環(huán)從大到小開始,但是連輸出也沒有,報錯
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
舉報
2017-07-28
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]);
? ? ? ? ? ? }
? ? ? ? }
? ? }
/*這是我的代碼,參考下吧。*/
2017-07-13
倒序遍歷也不行
報錯
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
Arrays.sort(scores);
然后倒序遍歷