所以我需要一些帮助来获得我的程序的正确平均值。它不准确,因为它是总数/总数,但我需要将它除以两个最低值min和max。这是我的代码的必要部分供参考。
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);
}
}
答案 0 :(得分:0)
所以我需要一些帮助来获得我的程序的正确平均值。它不准确,因为它是总数/总数,但我需要将它除以两个最低值,分钟和最大值。
1“正确平均值”通常表示sum / number_of_elements
2“我需要将它除以两个最低值,min和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