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 , 73}
System.out.println(" 本次考试前三名是:");
/*调用并接收方法的返回值
将cjFf方法的返回值保存到scor数组
*/
int scor[]=hello.cjFf(scores);
for(int y=0;y>2;y++){ //遍历接收返回值的数组
System.out.println(scor[y]) //分别输出返回数组的每个值
}
}
//定义方法完成成绩排序并输出前三名的功能
public static int[] cjFf(int[] scores){ //创建一个cjFf方法参数为scores数组
int a=0; //用于记录有效成绩数量
Arrays.sort(scores); //将数组scores排序
int score=new int[2]; //新建一个int型数组用于返回值
for(int i=scores.length-1;i>=0;i--){ //从大到小的顺序遍历scores数组
if(a=3){ //判断有效成绩是否已经满足3个
continue; //满足直接跳出for循环
}
else //不满足继续赋值
{
if(scores[i]<0||scores[i]>100) //判断数组的成绩满足要求不
score[a]=scores[i]; //满足要求将数组的值赋予将返回的数组
a=a+1; //有效成绩加一
}
}
return score;//将数组返回
}
}
新建一个int型数组用于返回值
int score=new int[3];
看图片。