【发布时间】:2020-03-11 22:38:25
【问题描述】:
我随机生成了一个 1 到 100 之间的 50 个数字的列表,并将其写入一个新文件。我将如何读取文件并计算所有数字的总和和平均值?
import java.util.*;
import java.io.*;
import java.io.FileWriter;
import java.io.PrintWriter;
public class Lab5Part2{
public static void main(String [] args)throws IOException
{
FileOutputStream fo = null;
PrintWriter out = null;
File file = new File("outwrite.txt");
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
Random rand = new Random();
int number, count=0, countTwo=0;
while(count < 50)
{
while(countTwo < 50)
{
number=rand.nextInt(100)+1;
pw.println(number);
count++;
countTwo++;
}
}
pw.close();
}
}
【问题讨论】:
-
给定的代码有什么问题?
-
so many ways 读取文件