【发布时间】:2017-12-07 05:43:19
【问题描述】:
我正在尝试编写一个 Java 程序来在读取一个充满浮点数的文本文件后计算最大值、最小值、平均值和标准偏差。如您所见,我计算了最大值、最小值、平均值,但对于标准偏差,我感到很困惑。有什么想法我应该如何实现吗?
另外,我对编程还很陌生,如果结构不正确,非常抱歉。
这是我的代码:
/*
* Create a Java program to read a file of floating point numbers and compute
* the following statistics from the data file:
*
* 1. Maximum
* 2. Minimum
* 3. Arithmetic Average (Mean)
* 4. Standard Deviation
*
* Do not assume anything about the large numbers in the file. They could be
* positive or negative, and their magnitude could be extremely large or
* extremely small.
*/
import java.io.*;
import java.util.Scanner;
public class DataFile {
public static void main(String[] args) {
// declare variables
double number, maximum, minimum, sum, mean, standardDeviation;
int count;
Scanner file = null;
/* -------------------------------------------------------------------------- */
try {
file = new Scanner(new File("RawData.txt"));
}
catch(FileNotFoundException e) {
System.out.print("Error; The program was terminated!");
System.exit(1);
}
/* -------------------------------------------------------------------------- */
// initialize variables
maximum = file.nextDouble();
minimum = maximum;
sum = 0;
count = 1;
while(file.hasNextDouble()) {
number = file.nextDouble();
if(number > maximum)
maximum = number;
else if(number < minimum)
minimum = number;
sum += number;
count += 1;
} // end while loop
file.close();
/* -------------------------------------------------------------------------- */
// mean calculation
mean = sum / count;
// standard deviation calculation
// .....
// display statistics
System.out.println("Maximum ------------> " + maximum );
System.out.println("Minimum ------------> " + minimum );
System.out.println("Sum ----------------> " + sum );
System.out.println("Count --------------> " + count );
System.out.println("Mean ---------------> " + mean );
} // end method main
} // end class DataFile
【问题讨论】:
-
你知道计算是什么吗?还是您只是在问如何编码?
-
对不起。我想知道如何编码。我有 sigma 表示法的公式,这让它有点难。
-
也计算均方,然后使用所谓的标准差计算公式。
标签: java file text-files standard-deviation