輸出結果不對,能過。這是。。。
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};
? ? ? ? int []getscores=hello.getScores(scores);
? ? ? ? for (int i = 0; i < getscores.length; i++) {
System.out.println(getscores[i]);
}
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] getScores(int [] scores){
? ? ? ? int count=0;
? ? ? ? int []No3=new int[3];
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ??
? ? ? ? ? ? if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? count++;
? ? ? ? ? ? if(count>3){
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? else {
? ? ? ? ? ? No3[count--]=scores[i];
? ? ? ? ? ? }
? ? ? ? ??
? ? ? ? ? ?}
? ? ? ??
? ? ? ?return No3;
? ? }
}
2020-02-05
這樣寫不就好了么,用ArrayList而不是List
2020-02-03
只說你的結果為啥不多奧,其他的先不說,首先看到你的count++,系統在執行完這條語句之后,count變為了1,好,因為這里的count等于1,所以執行else里的語句:count--,這里又把count降為了0,然后結束了本次循環,下次循環再執行到count++時,由于count已經被降為0了,所以count又等于1,然后進入了一個假性的死循環,一直執行完了整個for循環,所以最后52被賦值給了No3[1],為啥是下標是1呢,因為count--等于count還沒改變的值,就等于1。反正我是不知道改啥了,想過一個思路吧,最好不要count++,count--一起使用。
2020-02-03
直接sort,然后reverse,再用下標輸出
2020-01-19
主函數里的for循環不需要,在你getscores里加個輸出語句就好了,
count為什么是count--,不需要用數組來保存得到的3個數,直接輸出就好了。