我有一个关于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