import java.util.Arrays;
public class Test3 {
//完成 main 方法
public static void main(String[] args) {
int[] scores={89,-23,64,91,119,52,73};
Test3 a=new Test3();
System.out.println("考试前三名的成绩为");
a.show(scores);
}
//定义方法完成成绩排序并输出前三名的功能
public void show(int[] scores){
int[] newScores = new int[5];
for(int i =0;i<scores.length;i++){
int n=0;
if(scores[i]>=0&&scores[i]<=100){
newScores[n]=scores[i];
n++;
}
}
Arrays.sort(newScores);
for(int i=0;i<5;i++){
int n=newScores[i];
System.out.println(n);
}
}
}
for(int i =0;i<scores.length;i++){
int n=0; // 这里每次循环都会让n = 0即使你后面把n++,一到这里又会让n=0
if(scores[i]>=0&&scores[i]<=100){
newScores[n]=scores[i]; //这里每次都是让newScore[0]=scores[i]
n++;
}
}
使用你新建的数组newScore里面的内容是{73,null,null,null,null}
package Day09;
import java.util.Arrays;
public class day0904 {
public static void main(String[] args) {
int[] a = new int[5];
a[0] = 73;
System.out.println(Arrays.toString(a)); //输出为[73,0,0,0,0]
}