慕粉2239006113
2017-03-01
5 回答
public class HelloWorld {
public static void main(String[] args) {
HelloWorld hello = new HelloWorld();
int[] scores = { 89, -23, 64, 91, 119, 52, 73 };
hello.list(scores);
}
public void list(int []scores) {
Arrays.sort(scores);
int count = 0;
int []topThree = new int [3];
for(int i = scores.length-1;i>=0;i--){
if (scores[i] < 0 || scores[i] >100)
continue;
topThree[count] = scores[i];
count++;
if(count == 3)
break;
}
System.out.println(Arrays.toString(topThree));
}
}
试试这个咯
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(); System.out.println(Arrays.toString(getArray(scores))); } //定义方法完成成绩排序并输出前三名的功能 public static int[] getArray(int[] array) { Arrays.sort(array); int count=-1; int[] newArray=new int[3]; for(int i=array.length-1;i>=0;i--) { if(array[i]<0||array[i]>100) { continue; } count++; newArray[count]=array[i]; if(count==2) { break; } } return newArray; } }
我不知道写的好不好,以后再回来改吧
举报
0/150
提交
取消