我正在编写一个提示用户输入文件名的程序。我必须假设该文件包含一个表示 n 个数据值数量的整数,然后是一系列 n 个浮点数(在一个名为 RandomFloats 的文件中给我),每个都写在单独的行上。该程序应该将 RandomFloats 文件的内容读入一个数组,然后在控制台中显示并写入文件以下数据:数组中浮点数的个数,数组中的最低和最高数,总数和平均值。到目前为止,这是我的代码(我将在不起作用的部分添加评论)
import java.io.File;
import java.util.*;
public class Problem9 {
/**
* @param args
*/
public static void main(String[] args) {
File rf = new File("RandomFloats");
Scanner kb = new Scanner(rf); //Unhandled exception type FileNotFoundException
double[] numFloats = new double [4268];
for (int i = 0; i < numFloats.length; i++){
numFloats[i] = kb.nextInt();
}
System.out.println("Please enter a file name");
String fileName = kb.nextLine();
minValue (numFloats);
maxValue (numFloats);
totalValue (numFloats);
averageValue (numFloats);
}
public static void minValue (double[] numFloats){
double min = 0;
for(int i = 0; i < numFloats.length; i++){
if (numFloats[i] < min){
min = numFloats[i];
}
}
System.out.println("Min Value: " + min);
}
public static void maxValue (double[] numFloats){
double max = 0;
for(int i = 0; i < numFloats.length; i++){
if (numFloats[i] > max){
max = numFloats[i];
}
}
System.out.println("Max Value: " + max);
}
public static void totalValue (double[] numFloats){
double total = 0;
for (int i = 0; i < numFloats.length; i++){
total += numFloats[i];
}
System.out.printf("\nTotal1: %.1f" , total);
}
public static void averageValue (double[] numFloats){
double total = 0;
double average;
for (int i = 0; i < numFloats.length; i++){
total += numFloats[i];
}
average = total / numFloats.length;
System.out.printf("\nAverage: %.1f" , average);
}
}
我不确定如何打印出样本大小,并且我创建的文件 RandomFloats 没有被读入数组。请帮助我,我完全被卡住了,谢谢!