為什么 i=scores.length-1 要減一,是不是因?yàn)閿?shù)組是重零開始的原因
public static void main(String [] args){
int [] scores = {89, -23, 64, 91, 119, 52, 79};
System.out.println("本次考試前三名:");
HelloW hello = new HelloW();
hello.top3(scores);
}
public void top3(int [] scores){
Arrays.sort(scores);
int num=0;
for(int i=scores.length-1;i>=0 && num<3;i--){
if(scores[i]<0 || scores[i]>100){
continue;
}
num++;
System.out.print(scores[i]+",");
}
}
2017-04-10
是的,?i=scores.length是數(shù)組的長度,而數(shù)組的下標(biāo)是從0開始的
2017-04-09
恩,是的