2013-05-03 80 views
-1

我有一個關於java的項目,它希望我能夠編寫一個控制檯程序,該程序讀取一行中的數字分數列表,並用空格分隔,丟棄最高和最低的數字,然後計算餘數的算術平均值。如何製作數字列表來計算餘數的算術平均值?

我有問題如何使數組列表中的數字來計算他的算術平均值。

這些代碼:

其實這些代碼的Java Applet和Java應用程序工作,但我可以用掃描方法的數字,但我無法找到一個解決辦法,以便爲數字列表。

public static void main(String[] args) { 

    /* 
    * For data input. 
    */ 
    Scanner kb = new Scanner(System.in); 

    /* 
    * Declare an array to store the judge's scores. 
    */ 
    double[] scores = new double[8]; 

    /* 
    * Declare variables for lowest and highest scores. 
    */ 
    double lowestScore = Integer.MAX_VALUE; 
    double highestScore = Integer.MIN_VALUE; 

    /* 
    * Read 7 judge's scores. 
    */ 
    for (int i = 0; i < 7; i++) { 
     System.out.println(String.format("Judge Score #%d: ", i + 1)); 
     scores[i] = kb.nextDouble(); 

     /* 
     * Compare the current score with the lowest and the highest scores. 
     */ 
     if (scores[i] < lowestScore) { 
      lowestScore = scores[i]; 
     } 
     if (scores[i] > highestScore) { 
      highestScore = scores[i]; 
     } 
    } 

    /* 
    * Sum the scores, except the lowest and the highest scores. 
    */ 
    double total = 0.00; 
    for (int i = 0; i < 7; i++) { 
     if (scores[i] != lowestScore && scores[i] != highestScore) { 
      total = total + scores[i]; 
     } 
    } 

    /* 
    * Display the output. 
    */ 
    DecimalFormat df = new DecimalFormat("0.00"); 
    System.out.println("Total points received: " + df.format(total)); 
    System.out.println("Arithmetic Mean : " + df.format(total/7)); 

} 

有沒有人有關於我的問題的另一個想法?

回答

0

其實從陣列中刪除的最低和最高的數字最簡單的方法是做到以下幾點:

Arrays.sort(names); 

然後要麼放棄0th element7th element或從陣列莫名其妙地刪除它們。

/* 
* Read 7 judge's scores. 
*/ 
for (int i = 0; i < 7; i++) { 
    System.out.println(String.format("Judge Score #%d: ", i + 1)); 
    scores[i] = kb.nextDouble(); 
} 

Array.sort(scores); 

/* 
* Sum the scores, except the lowest and the highest scores. 
*/ 
double total = 0.00; 
for (int i = 1; i < 6; i++) { 

     total = total + scores[i]; 

} 
+1

這種方式是'O(n log n)'的複雜性。做這個沒有排序就是'O(n)' – durron597 2013-05-03 21:10:28

+2

爲什麼當他插入我的例子並觀察它的工作時會過於複雜? – 2013-05-03 21:15:47

0

讀了分數,你只需要幾行:

Arrays.sort(scores); 
double total = 0; 
for (double score : Arrays.copyOfRange(scores, 1, 6)) 
    total += score; 
double average = total/6; 

注意,沒有必要知道什麼最高/最低分數,所以所有的相關的代碼或這個我們無關緊要,應該刪除。此外,沒有必要保留對子數組的引用,只是爲了遍歷它,所以爲了簡潔起見,Arrays.copyOfRange()的結果在foreach循環中被內聯使用。


當納入你的計劃,你的整個方法可歸納爲以下:

public static void main(String[] args) { 
    Scanner kb = new Scanner(System.in); 
    double[] scores = new double[8]; 
    for (int i = 0; i < 7; i++) { 
     System.out.println(String.format("Judge Score #%d: ", i + 1)); 
     scores[i] = kb.nextDouble(); 
    } 

    Arrays.sort(scores); 
    double total = 0.00; 
    for (double score : Arrays.copyOfRange(scores, 1, 6)) 
     total += score; 

    DecimalFormat df = new DecimalFormat("0.00"); 
    System.out.println("Total points received: " + df.format(total)); 
    System.out.println("Arithmetic Mean : " + df.format(total/6)); 
} 

另外請注意,您的算術錯誤:總應該由找到分平均值不是7,因爲總計有6個分數(兩個極端分數少於8個)。

+0

首先,感謝您的幫助,但我可以在哪裏添加此示例到我的程序中? – mertha 2013-05-04 10:49:21

+0

查看編輯答案,瞭解如何將我的代碼整合到您的方法中。 – Bohemian 2013-05-04 11:43:15

+0

我將這個示例添加到我的程序中,但是我不能正常工作,因爲當我在Java Applet中運行此程序時,我無法看到寫入數字列表的任何內容,但它正確運行在Java應用程序中。 號碼列表示例:「11 23 45 4 5 33 77」。 在我的程序中,用戶編寫數字列表(如上所述)。然後,程序應該丟棄77和4(因爲它們是列表中最大和最小的數字)。最後,程序應該計算算術平均值「11 23 45 5 33」。 我該怎麼做? 感謝您的幫助。 – mertha 2013-05-04 13:16:12