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