循环和文件统计Java程序
我很难写这个程序。我对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();
下面是数字中的几行。这个程序应该从中读取的文本
87.5517
72.14015
88.24248
60.524
65.38684
94.48039
84.73287
84.74978
73.78996
# 1 楼答案
FileReader
。你为什么用PrintWriter
?一个用于输入(读取文件),另一个用于输出(写入文件)李>inputFile
的变量李>threeDecimals
对象。它是DecimalFormat类型。可以使用threeDecimals.format(number)
格式化字符串李>一般来说,所有标准类型中可用的所有方法都在Java SE API Documentation中