我正在为学校做一份复习工作.赋值是编写一个类,它从标准输入读取一个包含几个整数的文件,这些整数将被放入一个数组中.从这里开始,需要编写方法来找出平均值,中位数,最大值,最小值和标准差.
它读起来像这样:
45
56
67
78
89
等等…
所以,我假设我需要创建一个数组列表(因为长度未定义)并使用scanner读取每一行的整数,然后创建将挑选我需要的方法.
但是,我无法理解如何正确使用FileReader和Scanner.
我目前正在运行BlueJ.文本文件位于项目文件夹下,但代码永远找不到该文件.
这是我到目前为止所拥有的.
import java.io.*;
import java.util.*;
import java.math.*;
public class DescriptiveStats
{
public DescriptiveStats(){}
public FileReader file = new FileReader("students.txt");
public static void main(String[] args) throws IOException
{
try{
List scores = new ArrayList();
Scanner sc = new Scanner(file);
while(sc.hasNext())
{
scores.add(sc.nextInt());
}
sc.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}