首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

好急哪位高手能帮小弟我写一下这程序阿?感激不尽阿!

2012-01-09 
好急啊!谁能帮我写一下这程序阿???感激不尽阿!!!writeaprogramthatwilltakenumbersfromafilecalledNumbers

好急啊!谁能帮我写一下这程序阿???感激不尽阿!!!
write   a   program   that   will   take   numbers   from   a   file   called   Numbers.txt   and   then   add   them   up,   and   give   out   their   average

[解决办法]
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadNumber {

  public static void main(String[] args) {
    
    File file = new File( "number.txt ");
    BufferedReader br = null;
    try {
      br = new BufferedReader(new FileReader(file));
      String temp;
      float sum = 0;
      int i = 0;
      while( (temp = br.readLine()) != null) {
        sum += Float.parseFloat(temp);
        i++;
      }
      System.out.printf( "平均值为:%.2f%n ", sum / i);      
    } catch (FileNotFoundException e) {      
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        br.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

number.txt内容:
14.3
15.7
38.9
145.9

热点排行