這個(gè)代碼真亂啊啊啊
錯(cuò)誤分析:函數(shù)返回時(shí)直接返回?cái)?shù)據(jù)就行,不需要再定義類型了,只有初始定義時(shí)候才需要在變量前加int[]這樣的類型。
而且你的sort函數(shù)的返回值應(yīng)該是 傳入的?scores,而不是前面過程中的nums
代碼如下:
import?java.util.Arrays;
public?class?HelloWorld?{
????//完成?main?方法
????public?static?void?main(String[]?args)?{
????????System.out.println("考試成績的前三名為:");
????????HelloWorld?hello?=?new?HelloWorld();
????????int[]?scores?=?new?int[]{89,?-23,?64,?91,?119,?52,?73};
????????int[]?nums?=?new?int[3];
????????nums?=?hello.sort(scores);
????}
????//定義方法完成成績排序并輸出前三名的功能
????public?int[]?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]);
????????}
????????return?scores;
????}
}
而你的這個(gè)代碼不夠優(yōu)化:
應(yīng)該這樣優(yōu)化:
import?java.util.Arrays;
public?class?HelloWorld?{
????//完成?main?方法
????public?static?void?main(String[]?args)?{
????????System.out.println("考試成績的前三名為:");
????????int[]?scores?=?new?int[]{89,?-23,?64,?91,?119,?52,?73};
????????int[]?nums?=?new?int[3];
????????nums?=?sort(scores);
????????for?(int?rank?:
????????????????nums)?{
????????????System.out.println(rank);
????????}
????}
????//定義方法完成成績排序并輸出前三名的功能
????static?int[]?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]);?這里是執(zhí)行函數(shù)體,不要再這進(jìn)行數(shù)據(jù)'使用'操作,應(yīng)該把獲得的數(shù)據(jù)返回去而不是在這列出來
????????}
????????return?scores;
????}
}
執(zhí)行結(jié)果為:
考試成績的前三名為:
-23
52
64
73
89
91
119
你的算法是反的了,sort函數(shù)已經(jīng)從低到高排序,你取最大數(shù)據(jù)并獲得一個(gè)數(shù)據(jù)數(shù)組應(yīng)該為:
import?java.util.ArrayList;
import?java.util.Arrays;
public?class?HelloWorld?{
????//完成?main?方法
????public?static?void?main(String[]?args)?{
????????System.out.println("考試成績的前三名為:");
????????int[]?scores?=?new?int[]{89,?-23,?64,?91,?119,?52,?73};
????????ArrayList<Integer>?nums?=?new?ArrayList();
????????nums?=?sort(scores);
????????for?(int?i=1;i<=nums.size();i++){
????????????System.out.println("第"+i+"名:"+nums.get(i-1));
????????}
????}
????//定義方法完成成績排序并輸出前三名的功能
????static?ArrayList<Integer>?sort(int[]?scores)?{
????????Arrays.sort(scores);
????????int?count?=?0;
????????ArrayList<Integer>?newTopScores?=?new?ArrayList<>();
????????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]);?這里是執(zhí)行函數(shù)體,不要再這進(jìn)行數(shù)據(jù)'使用'操作,應(yīng)該把獲得的數(shù)據(jù)返回去而不是在這列出來
//????????????把最大的添加到數(shù)組里保存起來
????????????newTopScores.add(scores[i]);
????????}
????????return?newTopScores;
????}
}
運(yùn)行結(jié)果為:
考試成績的前三名為:
第1名:91
第2名:89
第3名:73