我的代碼供大家參考一下
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.sort(scores);
???????
??? }
???
??? //定義方法完成成績排序并輸出前三名的功能
??? public void sort(int[] scores){
??????? Arrays.sort(scores);
??????? int count=0;
??????? for(int i = scores.length-1;i>=0;i--){
??????????? if(scores[i]<0||scores[i]>100){
??????????????? continue;
??????????? }
??????????? else{
??????????????? count++;
??????????? }
??????????? if(count>3)
??????????? break;
??????????? System.out.println(scores[i]);
??????? }
??????
??? }
???
???
???
???
???
???
???
???
???
}
2018-09-25
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();
? ? ? ? int results[] =? hello.getScore(scores);
? ? ? ? System.out.println(Arrays.toString(results));
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] getScore(int [] scores){
? ? ? ? int [] result=new int[3];
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=0;i<3;i++){
? ? ? ? ? ? if(scores[scores.length-i-1] <0){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? result[i] = scores[scores.length-i-1];
? ? ? ? ? ??
? ? ? ? }
? ? ? ? return result;
? ? }
? ??
? ??