import java.util.Arrays;public class HelloWorld { //完成 main 方法 public static void main(String[] args) { int[] scores={89 , -23 , 64 , 91 , 119 , 52 , 73}; HelloWorld hello=new HelloWorld(); hello.rank(scores); } //定义方法完成成绩排序并输出前三名的功能 public void rank(int[] scores){ int[] finalScor=new int [scores.length]; int i=0; Arrays.sort(scores); for(int score:scores){ if(score>=0&&score<=100){ finalScor[i]=score; i++; } } for(int j=i-1;j>i-4;j--){ System.out.println(finalScor[j]); } }}
3 回答
心钧
TA贡献11条经验 获得超2个赞
int[] scores = { 89, -23, 64, 91, 119, 52, 73 };
for (int i = 0; i < scores.length - 1; i++) {
for (int j = 0; j < scores.length - i - 1; j++) {
if (scores[j] < scores[j + 1]) {
int temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
int[] newscores;
newscores = Arrays.copyOfRange(scores, 0, 3);
for (int i : newscores) {
System.out.print(i+";");
}
这样也可以……
添加回答
举报
0/150
提交
取消