weibo_耗子12437133_0
2016-01-13 11:42:29
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.rank(scores);? ? }? ??? ? //定義方法完成成績排序并輸出前三名的功能 ? ?? ?public void rank(int[] scores){? ? ? ?int[] finalScor=new int [scores.length];? ? ? ?int i=0;? ? ? ?Arrays.sort(scores);? ? ? ?for(int score:scores){? ? ? ? ? ?if(score>=0&&score<=100){?? ? ? ? ? ? ? ?finalScor[i]=score;? ? ? ? ? ? ? ?i++;? ? ? ? ? ?}? ? ? ?}? ? ? ?for(int j=i-1;j>i-4;j--){? ? ? ? ? ?System.out.println(finalScor[j]);? ? ? ?}? ?}}
3 回答

心鈞
TA貢獻11條經驗 獲得超2個贊
int[] scores = { 89, -23, 64, 91, 119, 52, 73 };
?? ??? ?for (int i = 0; i < scores.length - 1; i++) {
?? ??? ??? ?for (int j = 0; j < scores.length - i - 1; j++) {
?? ??? ??? ??? ?if (scores[j] < scores[j + 1]) {
?? ??? ??? ??? ??? ?int temp = scores[j];
?? ??? ??? ??? ??? ?scores[j] = scores[j + 1];
?? ??? ??? ??? ??? ?scores[j + 1] = temp;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?int[] newscores;
?? ??? ?newscores = Arrays.copyOfRange(scores, 0, 3);
?? ??? ?for (int i : newscores) {
?? ??? ??? ?System.out.print(i+";");
?? ??? ?}
這樣也可以……
添加回答
舉報
0/150
提交
取消