求大神指點錯在哪
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};
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? int[] qiansan =hello.score(scores);
? ? ? ? for(int i=0;i<qiansan.length;i++)
? ? ? ? {
? ? ? ? ? System.out.println(qiansan[i]);
? ? ? ? }
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int score(int[] scores)
? ? {
? ? ? ? Arrays.sort(scores);
? ? ? ? int n=1;
? ? ? ? int[] num=new int[3];
? ? ? ? for(int i=scores.length-1;i>=0;i++)
? ? ? ? {
? ? ? ? ? ? if(scores[i]<0||scores[i]>100)
? ? ? ? ? ? continue;
? ? ? ? ? ? if(n<=3)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num[n-1]=scores[i];
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return num;
? ? }
}
2016-10-18
方法錯誤,int[] qiansan =hello.score(scores);這句都是多余的,很多代碼都是錯的哦,給你參考我的。A1是我自己取的,就是HelloWorld..
package?com.A; import?java.util.Arrays; public?class?A1?{ ?public?static?void?main(String[]?ages){ ?int[]?scores?=?{89,-23,64,91,119,52,73}; ?System.out.println("成績的前三名為:"); ?A1?Top3?=?new?A1(); ?Top3.showTop3(scores); ?} ?public?void?showTop3(int[]?scores){ ??Arrays.sort(scores); ??int?num?=?0; ???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]); ???} ?} }2016-10-18
編譯錯誤還是運行錯誤還是邏輯錯誤?
2016-10-18
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};
??????? System.out.println("考試成績的前三名為:");
??????? int[] qiansan =hello.score(scores);
??????? for(int i=0;i<qiansan.length;i++)
??????? {
????????? System.out.println(qiansan[i]);
??????? }
??? }
???
??? //定義方法完成成績排序并輸出前三名的功能
??? public int[] score(int[] scores)
??? {
??????? Arrays.sort(scores);
??????? int n=1;
??????? int[] num=new int[3];
??????? for(int i=scores.length-1;i>=0;i--)
??????? {
??????????? if(scores[i]<0||scores[i]>100)
??????????? continue;
??????????? if(n<=3)
??????????? {
??????????????? num[n-1]=scores[i];
??????????????? n++;
??????????? }
??????????? else
??????????? break;
??????? }
??????? return num;
??? }
}