錯誤太多,大家不要嘲笑小白啊,我完全是照著前面學的代碼照貓畫虎,完全不成樣子?。。?!
我不知道一個靜態的數組可以只輸出部分,所以我想把最先的那個靜態數組的排序后符合條件的前三名的值賦值給一個新的數組,然后完全凌亂了??!
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 places = hello.getRank(3);
? ? ? ? //調用方法并將返回的數組保存在變量中
? ? ? ? System.out.println(Arrays.toString(places));
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int getRank(int length){
? ? ? ? //帶參數的方法
? ? ? ? int[] places=new int[length];
? ? ? ? Arrays.sort(scores);
? ? ? ? //排個序
? ? ? ? for(int i=0;i<7;i++){
? ? ? ? ? ? if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? continue; ? ?
? ? ? ? ? ? }
? ? ? ? ? ? //遍歷數組把不符合條件的忽略
? ? ? ? ? ? places[0]=scores[0];
? ? ? ? ? ? places[1]=scores[1];
? ? ? ? ? ? places[2]=scores[2];
? ? ? ? ? ? //然后把scores數組的前三名賦值給places數組的前三名
? ? ? ? ? ? return palces;
? ? ? ? ? ? //然后返回places數組
? ? ? ? }
? ? ? ??
? ? }
2015-04-02
你的scores變量不是全局靜態變量,你要當成參數傳給getRank()方法,你的方法返回值也不對。
2015-04-02
你返回的是數組,那么你的place也應該是數組