繁体   English   中英

循环和文件统计Java程序

[英]Loops and File Statistics Java Program

我写这个程序很困难。 我对JAVA还是很陌生,我有基本的了解。 但是我似乎无法理解它要我做什么。

import java.text.DecimalFormat;
import java.util.Scanner;       
import java.io.*;           

public class StatsDemo
{
    public static void main(String [] args) throws IOException  
    {
        double sum = 0; 
        int count = 0;  
        double mean = 0; 
        double stdDev = 0;
        String line;
        double difference;  //difference between the value and the mean

        DecimalFormat threeDecimals = new DecimalFormat("0.000");
        Scanner keyboard = new Scanner (System.in);
        String filename;

        System.out.println("This program calculates statistics"
            + "on a file containing a series of numbers");
        System.out.print("Enter the file name:  ");
        filename = keyboard.nextLine();   

以下是该程序应从Number.txt中读取的一些行。

87.5517
72.14015
88.24248
60.524
65.38684
94.48039
84.73287
84.74978
73.78996
  • 要求您在注释中使用FileReader 为什么要使用PrintWriter 一个用于输入(从文件读取),另一个用于输出(写入文件)。
  • 您在任务5中正确进行了“启动”和while循环。 您为什么在任务4中没有做同样的事情? 您那里没有名为inputFile的变量。
  • 在这两种情况下,请关闭阅读器,而不要关闭阅读器后面的文件。 它将自动关闭其后面的文件。
  • 为了以正确的小数位数进行打印,必须使用提供的threeDecimals对象。 它是DecimalFormat类型。 您可以使用threeDecimals.format(number)格式化字符串。

通常,所有标准类型中可用的所有方法都在Java SE API文档中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM