我的代码有两个问题。我正在使用平均值。
首先,我正在努力寻找哪一周的平均值最高,然后输出平均值是多少。
其次,txt文件有39周,但运行我的代码时只计算了38周。
到目前为止,这是我的代码
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class TemperaturesWeek2 {
public static void main( String [] args ) throws IOException {
int count=0;
int counts=0;
File inputFile = new File ("C:/Users/Phillip/Documents/Temp/temperatures.txt .txt");
Scanner scan = new Scanner(inputFile);
while(scan.hasNext()) {
count++;
scan.nextLine();
String words = scan.next();
double num1 = scan.nextDouble();
double num2 = scan.nextDouble();
double num3 = scan.nextDouble();
double num4 = scan.nextDouble();
double num5 = scan.nextDouble();
double num6 = scan.nextDouble();
double num7 = scan.nextDouble();
double average = (num1 + num2 + num3+ num4+ num5+ num6+ num7) / 7;
if(average >= 90) {
counts++;
}
}
System.out.println("Temperature Statistics:");
System.out.println("Number of weeks: " + count);
System.out.println("Weeks with an average temperature of 90 or above: " + counts);
System.out.println("Highest average weekly temperature:");
}
}