我這種寫法相比答案那個更好,可以解釋一下嗎
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("考試成績的前三名位:");
? ? ? ? HelloWord hello = new HelloWord();
? ? ? ? hello.sort(scores);
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void sort(int[] arr){
? ? ? ? Arrays.sort(arr);
? ? ? ? int num = 0;
? ? ? ? int count = 0;
? ? ? ? for(int i = arr.length-1;i>=0;i--){
? ? ? ? ? ? if(count<3){
? ? ? ? ? ? ? ? num = arr[i];
? ? ? ? ? ? ? ? System.out.println(num);
? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? }
? ? ? ? }
? ? }
2017-03-13
import java.util.Arrays;
public class HelloWorld {
public void sortArrays(int[] nums){
int count=0;
Arrays.sort(nums);
for(int i=(nums.length)-1;i>=0;i--){
if(nums[i]>100||nums[i]<0) continue;
System.out.println(nums[i]);
count++;
if(count==3) break;
}
}
public static void main(String[] args){
int[] nums={89,-23,64,91,119,52,73};
HelloWorld helloworld=new ?HelloWorld();
helloworld.sortArrays(nums);
}
}
2017-03-10
這個方法并沒有判斷成績的有效性所以運行結(jié)果是119,91,81是不對的