我有一個關於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));
}
有沒有人有關於我的問題的另一個想法?
這種方式是'O(n log n)'的複雜性。做這個沒有排序就是'O(n)' – durron597 2013-05-03 21:10:28
爲什麼當他插入我的例子並觀察它的工作時會過於複雜? – 2013-05-03 21:15:47