问答详情
源自:7-1 编程练习

AC代码如下

import java.util.*;

public class HelloWorld {

public static void main(String[] args){

int[] scores = new int[]{89,-23,64,119,91,52,73};

int[] grade = new int[3];

HelloWorld hehe = new HelloWorld();

hehe.qsort(scores,grade);

System.out.println("考试成绩的前三名为:");

System.out.println(grade[0]);

System.out.println(grade[1]);

System.out.println(grade[2]);

}


private void qsort(int[] scores, int[] grade) {

// TODO Auto-generated method stub

int count = 0;

Arrays.sort(scores);

for(int i=scores.length-1;i>=0;--i){

if (scores[i]>=0&&scores[i]<=100) {

if (count>=3) {

break;

}

grade[count++]=scores[i];

}

}

}

}


提问者:墙角等候幸福 2015-08-19 09:59

个回答

  • bluesQi
    2015-12-15 14:09:46

    你的代码在 qsort的for循环多走了一遍,你可以将 grade[count++]=scores[i]; 放在 if语句上面