练习1-2:编写一个 JAVA 程序,实现输出考试成绩的前三名。

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

 

 
posted @ 2017-11-02 16:50  北有孤鹜  阅读(2393)  评论(1编辑  收藏  举报