计算文件中的多组int值

时间:2014-03-06 21:29:00

标签: java loops controls nested-loops

所以我需要输入一个包含这样的数据的文件

Joe Schmoe
76.5 20 30 25 10 75.9
Jilly Momilly Schmill
50.5 30 30 35 37 28 32 35 34 34 49.2
Gail Quail
62.3 15 17 10 3 10 63.6
Frank Crank
83.2 5 83.2
John Quann
57.9 30 32 35 32 30 57.2

我需要程序来读取数据并为每个人总结数据  Joe Schmoe out put的例子是

4   85  21.25   0.5999999999999943

是天数(总数),总分钟数(总金额),平均每天总和/天数,最终值是第一个双减去第二个。

我读取数据的算法是:

public void summaryData()
    {
        try 
        {
            while (inputStream.hasNext())
            {
                line = inputStream.nextLine();
                firstWeight = inputStream.nextDouble();
                while (inputStream.hasNextInt()) 
                {   
                            sum += inputStream.nextInt();
                    count ++;
                    avg = (sum/count);  
                }
                lastWeight = inputStream.nextDouble();
                        outputStream.println(count + "    " + sum + "    " + avg +  "    " + (lastWeight-firstWeight));
            if(inputStream.hasNext())
                    inputStream.nextLine(); 
            }
        }
        catch (InputMismatchException e) 
        {
            System.out.print(e);
            System.exit(0);
        }
    }

我的问题是我需要int计数和每个人的总和重新开始,而不是将它们全部加在一起。

非常感谢任何帮助或建议

示例输出:

4    85    21.0    -0.5999999999999943
13    380    29.0    -1.2999999999999972
18    435    24.0    1.3000000000000043
19    440    23.0    0.0
24    599    24.0    -0.6999999999999957

1 个答案:

答案 0 :(得分:1)

重置每个新行的计数

...
while (inputStream.hasNext()) {
  count = 0;
...

看起来你的第一个也是最后一个你的数学都被逆转了......

lastWeight-firstWeight

应该是

firstWeight-lastWeight