我重新发布此内容是因为我觉得我的措辞不正确
我正在尝试查找平均气温为 90 或以上的星期的百分比。
文本文件如下所示:
1/1/2009 76.0 81.1 68.1 86.7 99.2 97.5 92.9
1/8/2009 61.0 86.2 99.3 74.2 89.5 100.2 80.7
1/15/2009 95.7 76.2 92.1 66.5 66.7 80.2 76.3
1/22/2009 97.5 63.0 77.3 71.9 84.8 73.8 80.7
...总共 39 周
如何找到每周的平均气温?
我们应该使用 for 循环来读取 7 个温度,但我什至不知道如何开始。
这就是我目前所拥有的
while (scan.hasNextLine())
{
count++;
scan.nextLine();
}
for (
System.out.println("Temperature Statistics:");
System.out.println("Number of weeks: " + count);
System.out.println("Weeks with an average of 90 or higher: "+averageAbove);
最佳答案
您只是想简单地计算文本文件中的行数吗?
如果是这样,下面的代码可以做到这一点,但效率不是很高。
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int count = 0;
while ((line = br.readLine()) != null) {
count++
}
br.close();
System.out.println("Number of weeks: " + count);
关于java - 计算txt文件中的周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19940772/