2013-05-04 146 views
0

我想问一个关于readLine()方法的问题。我的项目是读取list中带有空格的数字,并计算算术平均余数,舍弃最小和最大数字。我尝试一些方法来阅读数字,但他们都是不正确的。如何在java中使用readLine()方法读取数字?如何在java中使用readLine方法读取数字列表?

P.S:计划应与acm Libary(控制台程序)工作

int [] inputArray ; ---> will this be helpful to me ? 

感谢你的帮助。


import java.text.DecimalFormat; 
import java.util.Scanner; 

import acm.program.ConsoleProgram; 

public class CCC extends ConsoleProgram { 

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)); 

} 

}


这是我的代码,我问stackedoverflow的人如何解决这个问题,但我没能解决它。在这个计划,我应该得到的号码列表来自用户。例如;用户写一个列表“11 23 4 55 66 7”(数字之间有空格)。我的​​程序应该丢弃最大和最小的数字来计算算术平均值。我用扫描器方法成功完成它,如你所见,但是老师希望我用ACM libary(consoleprogram)编写这个程序。这个程序像Java Application一样成功运行,但是当我想在Java Applet中运行它时,我无法做到这一点。

感谢您的帮助和理解。


有没有人能帮助我?

+0

向我们展示您的方法和您遇到的困难。 – 2013-05-04 11:34:24

+0

@AchintyaJha我添加了一些关于我的程序的信息。 – mertha 2013-05-04 13:07:31

+0

@jlordo我添加了一些关于我的程序的信息,它解释了我昨天的问题。 – mertha 2013-05-04 13:08:19

回答

0

使用输入行,然后将它们转换使用int i = new Integer(myString).intValue();

+4

这样就不必要地创建一个对象,我改用'Integer.parseInt'。 – 2013-05-04 11:28:53

+0

@Satya我认为这种方式很好,但我怎么能写我的程序(你也可以看到有关我的程序上面的新信息 – mertha 2013-05-04 13:10:18

1

为int如果你要使用BufferedReader.readLine(),你需要

  • 修剪线和分割值
  • 使用Long.parseParse orDouble.parseDouble作为approriate。
  • 将每个值放入列表中
  • 排序列表。
  • 丢弃的最高和最低
  • 总和值留下
  • 除以列表的大小,得到的平均值。
+0

真的需要排序列表吗? – sjas 2013-05-04 11:53:23

+0

@sjas实际上,它不是必要的,我也是添加关于我上面的程序的一些信息 – mertha 2013-05-04 13:09:17

+0

我不必使用BufferedReader,但我会尝试。我可以如何更改我的代码为bufferedReader?@Peter Lawrey – mertha 2013-05-04 13:49:11