使用方法文件I / O.

时间:2016-11-11 19:33:20

标签: java loops methods file-io

在我的程序中,使用扫描仪获取用户输入文件名。用户在控制台中输入.txt文件名,我也在控制台输出,但现在如果我想用这种方法做这个程序:

 public static double deviation(File inputFile)

如何实施我的计划?

public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        double number = 0;
        double n = 0;

        double sum = 0;
        double sum1 = 0;
        int count = 0;
        double mean = 0;


            Scanner Scanscan = new Scanner(System.in);
            System.out.print("Enter the name of the file: ");
            String filename = Scanscan.nextLine();

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


            while(reader.hasNext()){
                number = reader.nextDouble();
                sum = sum + number;
                sum1 = sum1 + Math.pow(number,2);
                n++;
            }

            double n1 = Math.sqrt(n);
            double sum11 = Math.sqrt(sum1);
            mean = sum / n;

            double n12 = Math.pow(n,2) - n;
            double n22 = Math.pow(sum, 2);
            double x = ( n * sum1 - n22) / (n12);
            double deviation = (Math.sqrt(x));

            System.out.println( "The standard deviation of the values in this file is: " + deviation);

        }

1 个答案:

答案 0 :(得分:1)

将必要的部分移动到新方法中。

*code