在 java 中获得平均值,但存在不同的值

getting average in java with difference of values

所以我只需要一些帮助来为我的程序获得正确的平均值。它不准确,因为它是总和/总计数,但我只需要将它除以两个最低值,最小值和最大值。这是我的代码的必要部分以供参考。

int count = 0;
        int totalVal;

        while (input.hasNext()) {

            String tdate = input.next();
            String ttime = input.next();
            int tdata = input.nextInt();
            count++;

            if (tdata <= smallest) {
                smallest = tdata;
                minDate=tdate;
                minTime=ttime; 
            }

            if (tdata >= largest) {
                largest = tdata;
                maxDate=tdate; 
                maxTime=ttime; 


            }

        }

        double sum = smallest + largest;
        double average = sum / count;

        System.out.println("\n" + count); 
        System.out.println("Minimum: " + smallest + "@" +  minDate +" " + minTime);
        System.out.println("Maximum " + largest  + "@" + maxDate +" " + maxTime); 
        System.out.println("%.2d" + average);

    }

}

So I just need some help with getting the correct average for my program. It is not accurate because it is sum/ the total count but I need to just have it be divided by the two lowest values, min and max.

1 "correct average" 通常表示 sum / number_of_elements

2 "I need to just have it be divided by the two lowest values, min and max." 两个最低值的最小值和最大值?或者所有值的最小值和最大值?

我假设您需要将总和除以所有值的最小值和最大值。您需要计算实际总和并除以smallest + largest,而不是设置sum = smallest + largest。示例:

int count = 0;
int totalVal = 0; // don't need this variable
double sum = 0.0;

while (input.hasNext()) {
    String tdate = input.next();
    String ttime = input.next();
    int tdata = input.nextInt();
    sum += tdata;
    count++;

    if (tdata <= smallest) {
        smallest = tdata;
        minDate=tdate;
        minTime=ttime; 
    }

    if (tdata >= largest) {
        largest = tdata;
        maxDate=tdate; 
        maxTime=ttime; 
    }

}

double average = sum / (double) (smallest + largest);

// the rest of your code