package com.输出前三名;
import java.util.Arrays;
public class HelloWorld
{
//完成 main 方法
public static void main(String[] args)
{
int[] scores = {89 , -23 , 64 , 91 , 119 , 52 , 73} ;//成绩数组
System.out.println("考试成绩前三名为:");
HelloWorld hello = new HelloWorld(); //创建对象,名为hello
hello.showTop3(scores); //调用方法,传入成绩数组
}
//定义方法完成成绩排序并输出前三名的功能
public void showTop3(int[] scores)
{
Arrays.sort(scores); //用Arrays.sort()方法实现数组排序
int num = 0 ; //保存有效成绩的数量
for(int i = scores.length - 1 ; i >=0 ; i--) //倒叙遍历数组中的每一个元素
{
if(scores[i] < 0 || scores[i] > 100) //判断成绩的有效性
continue ; //如果成绩无效,则跳出本次循环,忽略此成绩
}
num++ ; //有效成绩加一
if(num > 3) //判断有效成绩的数量
{
break ; //如果有效成绩大于3 ,则结束循环,只输出前三名的成绩
}
System.out.println(scores[i]); //一次输出前3名成绩
}
}