我一直在练习我在网上下载的教科书中的问题,我在书中做了一个程序,要求编写一个搜索数字文件并显示最大数量,最小数量和平均值的程序。所有的数字。该问题还说使用了使用writeDouble编写的double类型的数字。
除了获得平均值之外,我已经能够满足所有要求。我知道当我试图得到数字的平均值时我尝试的是不正确的,但我想知道是否存在我所做的事情的效果,或者如果没有,我如何在不使用的情况下获得数字的平均值数组?这是我的代码
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter file name to be written");
String file=keyboard.next();
File fileName=new File(file);
try{
FileOutputStream fo=new FileOutputStream(fileName);
ObjectOutputStream output=new ObjectOutputStream(fo);
System.out.println("Enter numbers");
System.out.println("enter q to quit");
String number;
do
{
number=keyboard.next();
if(number.equals("q")){
output.writeUTF(number);
}
else{
double new_number = Double.parseDouble(number);
output.writeDouble(new_number);
}
}
while (!number.equals("q"));
if(number.equals("q")){
System.out.println("numbers were written to: "+fileName);
output.close();
fo.close();
}
}
catch(FileNotFoundException e){
}
catch(IOException e){
}
double num=0;
double num2=0;
double num_check=0;
double num3=999999999999999999999.99999;
int count=0;
System.out.println("Now the program starts");
try{
FileInputStream fi=new FileInputStream(fileName);
ObjectInputStream input=new ObjectInputStream(fi);
long file_length=fileName.length();
for(int i=0;i<file_length;i++){
num=input.readDouble();
if(num>=0){
System.out.println(num);
}
if(num>num_check){
num_check=num;
}
else if(num<num3){
num3=num;
}
count++;
//Here is my attempt at getting the average
double num_ave=num+num(i+1);
double average=num_ave/count;
System.out.println(average);
System.out.println("Average of all the numbers is: "+average);**
}
input.close();
fi.close();
}
catch(FileNotFoundException e){
}
catch(EOFException e){
System.out.println("Largest number: "+num_check);
System.out.println("Smallest number: "+num3);
}
catch(IOException e){
}
}
}
答案 0 :(得分:1)
FileInputStream fis = new FileInputStream(fileName);
try ( Scanner scanner = new Scanner( fis ) ){
double sum = 0;
int count = 0;
while( scanner.hasNextDouble() ){
double num = scanner.nextDouble();
count++;
sum += num;
}
double avg = sum/count;
}
答案 1 :(得分:0)
我认为这就是你想要做的事情
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter file name to be written");
String file=keyboard.next();
File fileName=new File(file);
try{
FileOutputStream fo=new FileOutputStream(fileName);
ObjectOutputStream output=new ObjectOutputStream(fo);
System.out.println("Enter numbers");
System.out.println("enter q to quit");
String number;
do
{
number=keyboard.next();
if(number.equals("q")){
output.writeUTF(number);
}
else{
double new_number = Double.parseDouble(number);
output.writeDouble(new_number);
}
}
while (!number.equals("q"));
if(number.equals("q")){
System.out.println("numbers were written to: "+fileName);
output.close();
fo.close();
}
}
catch(FileNotFoundException e){
}
catch(IOException e){
}
double num=0;
double num2=0;
double num_check=0;
double num3=Double.MAX_VALUE;
int count=0;
System.out.println("Now the program starts");
try{
FileInputStream fi=new FileInputStream(fileName);
ObjectInputStream input=new ObjectInputStream(fi);
double num_ave=0;
long file_length=fileName.length();
for(int i=0;i<file_length;i++){
num=input.readDouble();
if(num>=0){
//System.out.println(num);
num_ave+=num;
}
if(num>num_check){
num_check=num;
}
if(num<num3){
num3=num;
}
count++;
//Here is my attempt at getting the average
double average=num_ave/count;
System.out.println(average);
System.out.println("Average of all the numbers is: "+average);
}
input.close();
fi.close();
}
catch(FileNotFoundException e){
}
catch(EOFException e){
System.out.println("Largest number: "+num_check);
System.out.println("Smallest number: "+num3);
}
catch(IOException e){
}
}
答案 2 :(得分:-1)
良好做法是批量读取文件中的数据,关闭文件,然后处理数据。
文件长度以字节为单位,而不是双精度数。而不是使用计数器,使用while(input.hasNext())
如果您不能使用数组,则使用LinkedList或任何可迭代的Collection。如果那不是你想要的那么你可以通过排序做一些事情,虽然中位数可能很难。