我在编写这个程序时遇到了困难。我对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
的变量。 - 在这两种情况下,请关闭阅读器,而不是关闭其后面的文件。它会自动关闭后面的文件。
- 为了以正确的小数位数打印,您必须使用提供的
thirdDecimals
对象。它的类型为 DecimalFormat。您可以使用thirdDecimals.format(number)
设置字符串格式。
一般来说,所有标准类型中可用的所有方法都在 Java SE API Documentation 中。 。
关于java - 循环和文件统计 Java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26595605/