為什么一直運行不了啊 我感覺代碼都沒問題啊
?import java.util.Arrays;
public class HelloWorld?
{
? ?
? ? //完成 main 方法
? ? public static void main(String[] args)?
? ? {
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? int[] scores={89,-23,64,91,119,52,73};
? ? ? ? ? hello.num(scores);
? ? ? ?System.out.println("考試成績的前三名為:");
? ? ? ??
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? ? public void num(int[] scores)
? ? ? {
? ? ? ? ? Arrays.sort(scores);
? ? ? ? ? int x=0;
? ? ? ? ? for(int i=scores.length;i>=0;i--)
? ? ? ? ? {
? ? ? ? ? ? ? if(scores[i]>100||scores[i]<0)
? ? ? ? ? ? ? {continue;}
? ? ? ? ? ? ? if(scores[i]<=100&&scores[i]>=0)
? ? ? ? ? ? ? ?{x++;}
? ? ? ? ? ? ? if(x>3)
? ? ? ? ? ? ? { break;}
? ? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? }
? ? ? ? ??
? ? ? }
}? ? ?
2019-02-19
索引大于數(shù)組大小。 ?scores.length 改為scores.length-1就行了;
2019-02-17
for(int i=scores.length;i>=0;i--)?
這行代碼里面int i = scores.length 改為 int i = scores.length-1;
不然會超出數(shù)組最大值而出錯,數(shù)組是從0開始數(shù)的。
2019-02-17
for循環(huán)那里i忘記減1了