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();
????????hello.top3(scores);??????
????}
????
????//定義方法完成成績排序并輸出前三名的功能
????public?void?top3(int[]?scores){
????????Arrays.sort(scores);
????????System.out.println("考試成績前三名為:");
????????int[]?effScores;
????????int?count=0;
????????for(int?i=0;i<scores.length-1;i++){
????????????if(scores[i]<0||scores[i]>100){
????????????????continue;
????????????}else{
????????????????effScores[count]=scores[i];
????????????????count++;
????????????}
????????}
????????if(count>=3){
????????????for(int?j=count-1;j>count-4;j--){
????????????????System.out.println(effScores[j]);
????????????}
????????}else{
????????????System.out.println("有效成績不足三個(gè)!");
????????}
????}?
}
2015-10-22
int[]?effScores; 這里錯(cuò)了 不能這樣定義 ?定義數(shù)組必須明確其長度 比如?int[]?effScores=new int[3];