1

我已经解决这个问题好几天了,但无法让它发挥作用。问题要求我打开一个文件并计算平均值然后关闭它。然后重新打开它并计算标准偏差。我遇到的麻烦是在我计算平均值并关闭文件之后。当我尝试重新打开时,它显示找不到文件的错误。这是我到目前为止所拥有的:

File fr = new File(filename);
Scanner inputFile = new Scanner(fr);

filename = inputFile.nextLine();

while(inputFile.hasNext())
{
    double number = inputFile.nextDouble();
    sum =  number + sum;

    count++;

    line = inputFile.nextDouble();
}
inputFile.close();
mean = sum / count;

//New File

File file = new File(filename);
Scanner br = new Scanner(file);

double sumB = 0;
int countB = 0;

filename = br.nextLine();
while(br.hasNext())
{
    double sumthin = br.nextDouble();
    difference =  sumthin - mean;
    sumB = sumB + Math.sqrt(difference);
    count++;
    line = br.nextDouble();
}
br.close();
Math.sqrt(sumB / count);
4

1 回答 1

4

您(可能)没有两次打开同一个文件 - 您正在从第一个文件中读取文件名,然后打开第二个文件:

File fr = new File(filename);
...
filename = inputFile.nextLine();
...
File file = new File(filename);
于 2012-11-10T18:50:08.553 回答