//定義方法完成成績排序并輸出前三名的功能
import java.util.Arrays;
public class HelloWorld {
?? ?
??? //完成 main 方法
??? public static void main(String[] args) {
??? int [] scores={89 , -23 , 64 , 91 , 119 , 52 , 73};
??? System.out.println("考試成績的前3名為");
??? HelloWorld hello = new HelloWorld();
?? ?
??? hello.showTop3(scores);
?????? ?
?????? ?
??? }
?? ?
??? //定義方法完成成績排序并輸出前三名的功能
??? public void show(int[] scores){
??????? Arrays.sort(scores);
??????? int num=0;
??????? System.out.println(Arrays.toString(scores));
??????? for ( int i = scores.length - 1; i >= 0; i-- ) {
??????????? if (scores.[i]<0 || scores.[i]>100){
??????????? continue;
??????????? }
??????????? num++;
??????????? if(num>3){break;}
??????????? System.out.println(scores[i]);
???
為啥scores.length 需要-1,不太理解
2016-10-30
應(yīng)為scores.length代表的是數(shù)組的長度,比如int [] nums=new int[8],的時候nums.length就等于9,所以當然要-1拉,而文中的scores.length=9;
2016-10-21
知道了,數(shù)組的下標是從0開始的,第一步就數(shù)組越界啦