用return方法怎么寫
小伙伴們,請根據(jù)所學(xué)知識,編寫一個 JAVA 程序,實現(xiàn)輸出考試成績的前三名
要求:
1、 考試成績已保存在數(shù)組 scores 中,數(shù)組元素依次為 89 , -23 , 64 , 91 , 119 , 52 , 73
2、 要求通過自定義方法來實現(xiàn)成績排名并輸出操作,將成績數(shù)組作為參數(shù)傳入
3、 要求判斷成績的有效性( 0—100 ),如果成績無效,則忽略此成績
運行效果:
小伙伴們,請根據(jù)所學(xué)知識,編寫一個 JAVA 程序,實現(xiàn)輸出考試成績的前三名
要求:
1、 考試成績已保存在數(shù)組 scores 中,數(shù)組元素依次為 89 , -23 , 64 , 91 , 119 , 52 , 73
2、 要求通過自定義方法來實現(xiàn)成績排名并輸出操作,將成績數(shù)組作為參數(shù)傳入
3、 要求判斷成績的有效性( 0—100 ),如果成績無效,則忽略此成績
運行效果:
2017-11-26
舉報
2017-11-30
public static void main(String[] args) {
? ? ? ?int[] scores = {89,-23,64,91,119,52,73};
? ? ? ? HelloWorld hello = new ?HelloWorld();
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? int[] topScores = new int[3]; ?//新建一個數(shù)組來保存最高三個數(shù)組
? ? ? ?topScores = hello.highScore(scores);
? ? ? ?for(int i=0;i < topScores.length;i++)
? ? ? ? ? ?System.out.println(topScores[i]);
? ? ? ?
? ? ? ?
? ?}
??
? ?//定義方法完成成績排序并輸出前三名的功能
? ?public int[] highScore(int[] scores)
? ?{
? ? ? ?Arrays.sort(scores);
? ? ? ?int flag = 0;
? ? ? ?int[] topScores = new int[3];
? ? ? ?for(int i=scores.length-1;i>=0;i--)
? ? ? ?{
? ? ? ? ? ?if(scores[i]<=100 && scores[i]>=0)
? ? ? ? ? ?{
? ? ? ? ? ? topScores[flag] = scores[i]; ? //如果是最高的,就保存在這個長度為3的數(shù)組中,最后用作返回值。
? ? ? ? ? ? flag++;
? ? ? ? ? ?}
? ? ? ? ? ?if(flag == 3)
? ? ? ? ? ? break;
? ? ? ?}
? ? ? ?return topScores;
? ?}
2017-11-26
?//定義方法完成成績排序并輸出前三名的功能
??? public void ranking(int[] scores)
??? {
??????? int count = 0;
??????? for(int i = scores.length - 1;i >= 0; i--)
??????? {
?????????? ?
??????????? if(scores[i] < 100 && count <3)
??????????? {
??????????????? System.out.println("" + scores[i]);
??????????? }
??????????? if(count == 3)
??????????? {
??????????????? break;
??????????? }
??????? }
??? }
???