import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
HelloWorld hello=new HelloWorld();
int[] scores={89,-23,64,91,119,52,73};//将成绩放入数组中
hello.sorts(scores);
}
public void sorts (int []scores) {
int count=0;
Arrays.sort(scores);//对成绩进行排序
for ( int i = scores.length - 1; i >= 0; i-- ) {
if(scores[i]<0||scores[i]>100) {
continue;
}else {
System.out.println("成绩的前三名分别为:"+scores[i]);
count++;
}
if(count==3) {
break;
}
}
}
}
我觉得数组类型最好是double类型的,因为成绩可能会有小数,?
我是这么写的
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
HelloWorld output = new HelloWorld();
int[] scores = {89,-23,64,91,119,52,73};
output.compare(scores);
}
//定义方法完成成绩排序并输出前三名的功能
public void compare(int[] scores) {
Arrays.sort(scores);
for(int i=scores.length-1;i>scores.length-5;i--) {
if(scores[i]>0&&scores[i]<100) {
System.out.println(scores[i]);
}
}
}