Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算文件中的多组int值_Java_Loops_Controls_Nested Loops - Fatal编程技术网

Java 计算文件中的多组int值

Java 计算文件中的多组int值,java,loops,controls,nested-loops,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的例子是 4 85

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

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的例子是

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);
        }
    }
我的问题是,我需要每个个体的整数计数和总和重新开始,而不是将它们全部相加

非常感谢您的帮助或建议

样本输出:

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

重新设置每一行的计数

...
while (inputStream.hasNext()) {
  count = 0;
...
看起来你的数学第一次和最后一次都颠倒了

lastWeight-firstWeight
应该是

firstWeight-lastWeight

我的输出现在是48521.0-0.59999999999943 9 380 42.0-1.2999999999972 5 435 87.0 1.30000000000000043 1 440.0 0 0.0 5 599 119.0-0.699999999957这是lastWeight减去firstWeight。我们正在检查参与者是否减肥感谢帮助,我是否也重置了每个人的总数个人?同样的模式应该会起作用。。。在count=0后的行中添加“sum=0;”;