在不使用数组的情况下查找存储在一个变量中的输入值的平均值

Finding the average of entered values stored in one variable, without using an array

我一直在网上下载的课本上练习题,我在做书上的一个程序,要求编写一个搜索数字文件并显示最大数字、最小数字和最大数字的程序。所有数字的平均值。该问题还说要使用使用 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){  
    }
}

}

最好从文件中批量读取数据,关闭文件,然后处理数据。

文件长度以字节为单位,而不是双精度。而不是使用计数器,使用 while(input.hasNext())

如果您不能使用数组,则使用 LinkedList 或任何可迭代的集合。如果这不是你想要的,那么你可以通过排序来做一些事情,尽管中位数可能很难。

  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;
  }

我想这就是你想要做的

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){  
    }
}