在这个do while循环中,计算年平均温度、年低平均温度和年高平均温度的计算不能正常工作。
while (inFile.hasNext()) {
//extract Month name from file
month = inFile.next();
//Remove comma for better presentation
String formatMonth = month.replace(',', ' ');
//calculate high temperature from text file
highTemp = inFile.nextDouble();
//extract low temperature file from text file
lowTemp = inFile.nextDouble();
//Calculate averaget Temperature from values
averageTemp = (highTemp + lowTemp)/2;
//Calculate Range
range = (highTemp - lowTemp);
//Calculate low temperature
minTemp = Math.min(minTemp, lowTemp);
//Calculate high temperature
maxTemp = Math.max(maxTemp, highTemp);
//Sum up high temperatures
highSum =+ highTemp;
//Sum up low Temperatures
lowSum =+ lowTemp;
//calculate yearly sum
yearlySum =+ averageTemp;
//Calculate average of all the high temperatures
totalAvgHigh = highSum/12;
//Calculate average of all low temperatures
totalAvgLow = lowSum/12;
//Calculate the average of the years
totalAvgYearly = yearlySum/12;
//Calculate averge of the range
totalRangeAvg = (highSum +lowSum)/12;
System.out.println(formatMonth + " | " + highTemp + " | " + lowTemp + " | " + averageTemp + " | " + range );
outFile.println(formatMonth + " | " + highTemp + " | " + lowTemp + " | " + averageTemp + " | " + range );
}
//Print out calculated values
System.out.print("Average High Temps: " + totalAvgHigh + " Average Low Temps: " + totalAvgLow + " Total Average: " + totalAvgYearly + " Total Range Average: " + totalRangeAvg);
System.out.println("High Temp " + maxTemp);
System.out.println("Low Temp " + minTemp);
outFile.print("Average High Temps: " + totalAvgHigh + " Average Low Temps: " + totalAvgLow + " Total Average: " + totalAvgYearly + " Total Range Average: " + totalRangeAvg);
outFile.println("High Temp " + maxTemp);
outFile.println("Low Temp " + minTemp);
输出如下:
enter code here:
---------------------------------------------------------------------------------
Month | High Temperature | Low Temperature | Average Temperature | Range
---------------------------------------------------------------------------------
January | 72.0 | -12.0 | 30.0 | 84.0
February | 75.0 | -5.0 | 35.0 | 80.0
March | 80.0 | 8.0 | 44.0 | 72.0
April | 98.0 | 15.0 | 56.5 | 83.0
May | 98.0 | 29.0 | 63.5 | 69.0
June | 100.0 | 28.0 | 64.0 | 72.0
July | 103.0 | 48.0 | 75.5 | 55.0
August | 106.0 | 40.0 | 73.0 | 66.0
September | 102.0 | 35.0 | 68.5 | 67.0
October | 90.0 | 26.0 | 58.0 | 64.0
November | 85.0 | 14.0 | 49.5 | 71.0
December | 76.0 | -10.0 | 33.0 | 86.0
Average High Temps: 6.333333333333333 Average Low Temps: -0.8333333333333334 Total Average: 2.75 Total Range Average: 5.5High Temp 1.7976931348623157E308
Low Temp -12.0
尝试使用+=
代替=+
。