1、 考試成績已保存在數組 scores中,數組元素依次為 89 , -23 , 64 , 91 , 119 , 52 , 73
2、 要求通過自定義方法來實現成績排名並輸出操作,將成績數組作為參數傳入
3、 要求判斷成績的有效性( 0—100 ),如果成績無效,則忽略此成績
/*
小伙伴們,請根據所學知識,編寫一個 JAVA 程序,實現輸出考試成績的前三名
要求: 1、 考試成績已保存在數組 scores中,數組元素依次為 89 , -23 , 64 , 91 , 119 , 52 , 73
2、 要求通過自定義方法來實現成績排名並輸出操作,將成績數組作為參數傳入
3、 要求判斷成績的有效性( 0—100 ),如果成績無效,則忽略此成績
*/
package lianxi; import java.util.*; public class HelloWorld { public static void main(String[] args) { int[] scores={89,-23,64,91,119,52,73}; //創建一個數組 System.out.println("考試成績的前三名為:"); HelloWorld Fgrade=new HelloWorld(); //創建一個對象 Fgrade.grade(scores);//調用方法 } public void grade(int[] scores){ Arrays.sort(scores); //對數組進行升序排序 int count=1; for(int i=scores.length-1;i>=0&&count<=3;i--){ if(scores[i]>100||scores[i]<0) continue; else {count++; System.out.println(scores[i]); } } } }
操作結果:
考試成績的前三名為:
91
89
73