//功能: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};? ? ? ? System.out.println("考試前三名為:");? ? ? ? ?int[] halo=hello.sort(scores);? ? ? ? ?? ? ? ? for(int j:halo){? ? System.out.println(j);? ? }? ? ? ? ?? ? }? ??? ??? ? //定義方法完成成績排序并輸出前三名的功能? ? public int[] sort(int[] scores ){? ? ? ? int count=0;? ? ? ? Arrays.sort(scores);? ? ? ? //for循環應從后往前開始遍歷? ? ? ? for(int i=scores.length-1;i>=0;i--){? ? ? ? //判斷數組中的參數是否有效? ? ? ? ? ? if(scores[i]<0||scores[i]>100)?? ? ? ? ? ? ? ? continue;? ? ? ? ? ? ? ? count++;? ? ? ? ? ? if(count>3) break;? ? ? ? ?? ? ? ? }? ? ? ? ? ? ? return scores;? ? ??? ? }?}
我的本意是用返回類型為int[]去做,結果遍歷的數組halo只是把排序后的元素輸出出來了,如if語句都未執行
慕粉Javacoder
2016-06-03 20:24:19