為什么運(yùn)行不出來?
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? int[] scores={89,-23,64,91,119,52,73};
? ? HelloWorld hello=new HelloWorld();
? ? System.out.println("考試成績(jī)的前三名為:");
? ? hello.max(scores);
? ? ? ??
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ?public void max(int[] scores){
? ? ? ?
? ? ? ?Arrays.sort(scores);
? ? ? ?int b=0;
? ? ? ?for(int i=scores.length;i>=0;i--){
? ? ? ? ? ?if(scores[i]>100||scores[i]<0){
? ? ? ? ? ? ? ?continue;
? ? ? ? ? ?}
? ? ? ? ??
? ? ? ? ? ?b=b+1;
? ? ? ? ? ?
? ? ? ? ? ?if(b>3){
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?}System.out.println(scores[i]);
? ? ? ?}
? ?}
? ??
}
2015-12-20
for循環(huán)條件里面i的處置應(yīng)該為scores.length-1
2015-12-21
考試成績(jī)的前三名為:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at HelloWorld.max(HelloWorld.java:21)
at HelloWorld.main(HelloWorld.java:9)
for循環(huán)條件里面i的處置應(yīng)該為scores.length-1
2015-12-20
考試成績(jī)的前三名為:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at HelloWorld.max(HelloWorld.java:21)
at HelloWorld.main(HelloWorld.java:9)