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

为什么给数组赋值会失败,只赋入一个52

public static void main(String[] args) {

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

Arrays.sort(scores);

System.out.println(Arrays.toString(scores));

Number numThree= new Number();

int[] num_3=numThree.score(scores);

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

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

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

}

public int[] score(int[] scores){

int[] s=new int[3];

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

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

continue;

}

int j=0;

s[j]=scores[i];

j++;

if(j==3){

break;

}

}

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

return s;

}




提问者:banEden 2019-04-30 11:10

个回答

  • banEden
    2019-04-30 11:21:41

    给j赋值时应该放在循环外部,不然一直给s[0]赋值