【发布时间】:2015-11-09 17:55:16
【问题描述】:
package txtfileaverage;
import java.io.*;
import java.util.Scanner;
/**
*
* @author Frazer
*/
public class Txtfile {
public static void main(String args[]) throws IOException
{
Scanner file = new Scanner(new File("input.txt"));
int numTimes = file.nextInt();
file.nextLine();
for(int i = 0; i < numTimes; i++);
{
int sum = 0;
int count = 0;
Scanner split = new Scanner(file.nextLine());
while(split.hasNextInt())
//for (int a = 0; a < 4 ; a++)
{
sum += split.nextInt();
count++;
}
System.out.println("the average is = " + ((double)sum / count));
}
}
}
文本文件:
4
100 100 100 100
100 100 50 50
100 90 80 70
60 50 40 30
上面是我试图读取的文本文件,显示的输出是
“平均值为 100”,但它要么只查看一个数字,要么只查看 1 行,关于如何让它读取其他行的任何提示?我看过一些教程,在比较了代码之后,我很难找出为什么它只找到 1 个数字或 1 行而不是整行的平均值,每个都有另一个语句。
【问题讨论】:
标签: java file average filereader