永发信息网

准备一个文本文件,里面每行写一个数字,读进来,求平均值,java

答案:3  悬赏:20  手机版
解决时间 2021-02-25 00:43
准备一个文本文件,里面每行写一个数字,读进来,求平均值,java
最佳答案
public static void main(String[] args) throws Exception{
BigDecimal decimal = new BigDecimal(0);
int index = 0;
String path = "d:/remind.txt";
BufferedReader reader = new BufferedReader(new FileReader(path));
String line = "";
while((line = reader.readLine())!=null){
decimal = decimal.add(new BigDecimal(line));
index++;
}
System.out.println(decimal.divide(new BigDecimal(index),2,RoundingMode.HALF_UP));

reader.close();
}
全部回答
public class Demo { public static void main(String[] args) { File file = new File("D:/demo.txt"); try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line = null; int count = 0; int sum = 0; while ((line = reader.readLine()) != null) { System.out.print(line+","); sum += Integer.valueOf(line.trim()); count++; } System.out.println(); System.out.println("总个数为:  " + count + "        总和为:  " + sum + "      平均值为:    " + sum / count); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class Demo { public static void main(String[] args) { File file = new File("D:/demo.txt"); try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line = null; int count = 0; int sum = 0; while ((line = reader.readLine())
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
香满多好味道中式快餐我想知道这个在什么地方
长春市宽城区铁北二路的邮政编码
怎么让奥数学起来更有趣
世进电子有限公司山亭分公司地址在什么地方,
幕墙应使用的安全玻璃是()A.钢化玻璃B.半钢化
18个0.26是________,0.5的4倍是________.
学校考的电工证可以找工作吗
什么果冻最好吃
潍坊市潍城区符山水库管理局在什么地方啊,我
男孩个子生长高峰期是什么
京东郑州丰庆配送站地址有知道的么?有点事想
女生信息我起床咯 然后放猪猪的表情,这表示
一个挂钟,它1点钟打1下.2点打2下,.,12点钟打1
怎么我电脑主板有93度,CPU时显示35度时显示9
请问有知道奶粉3段和4段的区别
推荐资讯
每10股派10股派5元(含税) 是什么意思?
下列哪项贫血是由于红细胞破坏增多所致A.溶血
本田踏板车的这个LOCK拉手是干什么的?
流量卡是什么?贵么?是不是不可以打电话?
沾益县西平基督教堂在什么地方啊,我要过去处
男士背什么颜色包好看
我去年8月20号从农村信用设贷款出来,到现在
下列物质的有关用途,由其化学性质决定的是BA
阳朝村我想知道这个在什么地方
福汇集团的交易时间
大多数的细菌和真菌是生态系统中的A. 生产者B
仙人球花能吃吗
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?