这是新代码,感谢绅士帮助阐明了如何关闭文件流。我更改了代码,以便我的函数现在调用文件名,然后返回一个double。看起来不错。我不是因为程序在没有它的情况下是必要的,因为程序没有它,但我给出的指示指定每个函数应该调用文件(第二个函数calculateStandardDeviation应该调用文件和平均值)。我认为一切正常。再次感谢。
import java.util.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class Lab04
{
private static Scanner inFile;
private static Scanner open;
public static void main(String[] args) throws FileNotFoundException
{
inFile = new Scanner(new FileReader("input.txt"));
System.out.print("Input: ");
while(inFile.hasNextInt())
{
int input = inFile.nextInt();
System.out.print(input + " ");
}
inFile.close();
inFile = new Scanner(new FileReader("input.txt"));
File main = new File("input.txt");
Scanner open = new Scanner(main);
double mean = computeMean(open.nextDouble());
System.out.print("\nMean: ");
System.out.printf(String.format("%.2f", mean));
inFile.close();
inFile = new Scanner(new FileReader("input.txt"));
double standard = computeStandardDeviation(open.nextDouble(), mean);
System.out.printf("\nStandard Deviation: ");
System.out.printf(String.format("%.2f", standard) + "\n");
}
private static double computeMean(double mean)
{
int sum = 0;
int i = 0;
do
{
sum += inFile.nextInt();
i++;
}
while(inFile.hasNextInt());
mean = (double) sum/i;
return mean;
}
private static double computeStandardDeviation(double add, double mean)
{
add = 0;
double summation = 0;
int n = 0;
while(inFile.hasNextInt())
{
double calc = (inFile.nextInt() - mean);
add = Math.pow(calc, 2.0);
summation += add;
n++;
}
return Math.sqrt(summation/n);
}
}
答案 0 :(得分:2)
我的答案基于您从整数文件中读取的原始信息,而不是,因为您的代码会建议双倍,例如: while(inFile.hasNextDouble()){...
此外,我必须提出一些建议,以便该计划能够发挥作用(我认为)是有意的;我将从顶部开始按顺序排列。请耐心等待,因为我在底部包含了尽可能接近提供的工作代码副本。
package lab04.java;
其他任何内容,例如package lab04;
就足够了。import java.lang.*;
是未使用的导入System.gc();
您在运行inFile = new Scanner(new FileReader("input.txt"));
时已经重新启动了扫描仪。只需先关闭扫描仪。查看下面的代码示例。了解你正在通过“带有整数的文件”的扫描程序获得下一个(首先,因为我们重新声明inFile)。你命名这个双“文件”。然后,打印您要传递“file。”的computeMean方法的返回值。
double file = inFile.nextDouble();
System.out.printf("Mean: " + computeMean(file));
您的computeMean方法几乎正确(如下);但是,您不需要double mean
参数。基于此方法声明和computeStandardDeviation的声明,您需要了解两件事之一。 (A)这些方法既不是递归的,也不是必须的。 (B)返回类型是您在方法名称之前放置的类型,而不是括号中的内容。括号中的内容是提供给该方法的数据。在这种情况下,computeMean只需要input.txt中的整数,该整数由扫描程序inFile解析。因为inFile是一个类变量,所以computeMean已经可以访问它。现在,在computeMean的主体中,我们返回与(4)中相同的问题,即您在整数文件中请求下一个double。最终的代码示例反映了修复computeMean所需的更改以及对computeStandardDeviation的相应修改。
public static double computeMean(double mean) { double sum = 0; double i = 0; do { sum += inFile.nextDouble(); i++; } while(inFile.hasNextDouble()); mean = sum/i; return mean; }
package lab04; import java.util.*; import java.io.FileReader; import java.io.FileNotFoundException; public class Lab04 { private static Scanner inFile; public static void main(String[] args) throws FileNotFoundException { inFile = new Scanner(new FileReader("input.txt")); System.out.print("Input: "); while(inFile.hasNextInt()) { int input = inFile.nextInt(); System.out.print(input + " "); } inFile.close(); inFile = new Scanner(new FileReader("input.txt")); double mean = computeMean(); System.out.print("\nMean: " + mean); inFile.close(); inFile = new Scanner(new FileReader("input.txt")); System.out.print("\nStandard Deviation: " + computeStandardDeviation(mean)); } private static double computeMean() { int sum = 0; int i = 0; do { sum += inFile.nextInt(); i++; } while(inFile.hasNextInt()); double mean = (double) sum/i; return mean; } private static double computeStandardDeviation(double mean) { double add = 0; double summation = 0; int n = 0; while(inFile.hasNextInt()) { double calc = (inFile.nextInt() - mean); add = Math.pow(calc, 2.0); summation += add; n++; } return Math.sqrt(summation/n); } }
package lab04; import java.io.File; import java.util.*; import java.io.FileNotFoundException; public class Lab04 { public static void main(String[] args) throws FileNotFoundException { File file = new File("input.txt"); System.out.println("Input: "); print(file); System.out.print("\nMean: " + computeMean(file)); System.out.print("\nStandard Deviation: " + computeStandardDeviation(file)); } private static void print(File file) throws FileNotFoundException { Scanner inFile = new Scanner(file); while(inFile.hasNextInt()) { int input = inFile.nextInt(); System.out.print(input + " "); } inFile.close(); } private static double computeMean(File file) throws FileNotFoundException { Scanner inFile = new Scanner(file); int sum = 0; int i = 0; do { sum += inFile.nextInt(); i++; } while(inFile.hasNextInt()); inFile.close(); double mean = (double) sum/i; return mean; } private static double computeStandardDeviation(File file) throws FileNotFoundException { Scanner inFile = new Scanner(file); double add = 0; double summation = 0; int n = 0; while(inFile.hasNextInt()) { double calc = (inFile.nextInt() - computeMean(file)); add = Math.pow(calc, 2.0); summation += add; n++; } inFile.close(); return Math.sqrt(summation/n); } }