Java – looking for numbers greater than the average – why doesn’t my if statement work properly?

I'm testing a program that contains different text files of randomly generated numbers The Java program is built to add these numbers together from the text file, take the average value of these numbers, then (using the if statement) find the numbers greater than the average value from the text file, put the values into the ArrayList, and print the average value and ArrayList as output However, for some reason, when I run my program with different text files (I use two tests, one working and the other not currently) The results printed in the shell are incorrect - most of the values are greater than the average, but I get some no, and there are at most three

This is my code:

package homework.pkg23.average;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class Homework23Average {

    public static void main(String[] args) {
        ArrayList exes = new ArrayList ();
        double x = 0;
        double y = 0;
        Scanner inputStream = null;

        try {
            inputStream = new Scanner (new File ("MyInput.txt"));
        }
        catch (FileNotFoundException e) {
            System.out.println ("File not found,program aborted:");
            System.exit (1);
        }
        int count = 0;
        while (inputStream.hasNextDouble ()) {
            count ++;
            x = inputStream.nextDouble ();
            y += x;
            if (x > y/count) // x values greater than the mean
                exes.add (x);
        }
        System.out.println ("The value(s) greater than the mean," 
                            + y/count + ",are (is):");
        exes.forEach (System.out::println);
        inputStream.close ();
    }

}

When running from a file, my average is 79.67, but my output is as follows:

The value(s) greater than the mean,79.67,are (is):
128.0
93.0
143.0
111.0
95.0
116.0
136.0
129.0
141.0
78.0    <-- NOTICE: value is less than the average
93.0
105.0
90.0
90.0
144.0
116.0
136.0
138.0
75.0    <-- NOTICE: value is less than the average
80.0
126.0
75.0    <-- NOTICE: value is less than the average
80.0
98.0
114.0
116.0
86.0
78.0    <-- NOTICE: value is less than the average
123.0
145.0
103.0
111.0
91.0
134.0
119.0
91.0
121.0
113.0
129.0
91.0
116.0
85.0
85.0
126.0
145.0
98.0
115.0
83.0
127.0
119.0
97.0
125.0
121.0
123.0
86.0
108.0
100.0
134.0

I can't figure out why these values are falling I tested the program on another text file containing fewer input values, and everything was normal I'm new to Java because this is my second program after the "Hello world" program, and I don't have a wide knowledge of Java syntax

Solution

When you decide whether to add a number to the list of values greater than the mean, you can determine this decision based on the partial average of the numbers processed here This is why you see below the final average in the output element

For example, suppose the first element is 1 and the second element is 2 Then the average value of the first two elements is 1.5, and because 2 > 2 1.5, you can add it to the output list However, if the next element is greater than 2, the final average may be higher than 2, so your output will have a number lower than the final average

Your code can also make mistakes in the other direction - a number near the beginning of the input may be mistakenly considered below the average, even if it is above the final average

To get the correct output, you must do two iterations The first will calculate the average and store all input numbers, and the second will look for numbers higher than the average

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>