2013-05-03 79 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