java:loop从文件读取时产生不正确的输出?

时间:2015-02-16 06:34:43

标签: java file while-loop average scanning

此循环产生的小时数和支付金额的平均值,但输出在数学上是不正确的。如何编辑此代码以生成正确的平均小时值和平均付费值?

         Scanner openFile = new Scanner(inFile);
         while (openFile.hasNext()) {
                if (openFile.hasNextDouble()) {
                    totalHours += openFile.nextDouble();
                    numOfHourInputs++;
                }
                if (openFile.hasNextDouble()) {
                    totalPaid += openFile.nextDouble();
                    numOfCharges++;
                }
                else {
                    openFile.next(); }
            }

        averageHours = (totalHours/numOfHourInputs);
        averagePaid = (totalPaid/numOfCharges);

以下是我的档案: 第一列对于计算平均值并不重要。第二列包含小时数。第三栏包含费用。

此文件可以为用户添加更多数据 - 可以更改文件内部的值。 a 10.0 9.95 b 10.0 13.95 b 20.0 13.95 c 50.0 19.95 c 30.0 19.95

3 个答案:

答案 0 :(得分:1)

删除else:

else {
    openFile.next(); //this consumes all input
}

答案 1 :(得分:1)

以下代码

Double[][] values = {{10.0, 9.95},
        {10.0, 13.95},
        {20.0, 13.95},
        {50.0, 19.95},
        {30.0, 19.95}};

Double totalHours = 0.;
int numOfHourInputs = 0;
Double totalPaid = 0.;
int numOfCharges = 0;

for (final Double[] value : values) {
    totalHours += value[0];
    numOfHourInputs++;

    totalPaid += value[1];
    numOfCharges++;
}

final double averageHours = (totalHours / numOfHourInputs);
System.out.println("averageHours = " + averageHours);

final double averagePaid = (totalPaid / numOfCharges);
System.out.println("averagePaid = " + averagePaid);

产生了结果

averageHours = 24.0
averagePaid = 15.55

所以它显然不是一个数学问题。检查输入代码,尤其是行

openFile.next();

答案 2 :(得分:1)

你仍然需要跳过第一个令牌但是在正确的位置:

public static void main(String[] args) 
{
    double totalHours = 0.0;
    int numOfHourInputs = 0;

    double totalPaid = 0.0;
    int numOfCharges = 0;

    Scanner openFile = null;

    try
    {
        openFile = new Scanner(new File("c:\\temp\\pay.txt"));
    }
    catch (Exception e)
    {
        throw new RuntimeException("FNF");
    }

    try
    {
        while (openFile.hasNext()) 
        {
            // skip the first token
            String token = openFile.next();

            if (openFile.hasNextDouble()) 
            {
               totalHours += openFile.nextDouble();
               numOfHourInputs++;
            }
            if (openFile.hasNextDouble()) 
            {
               totalPaid += openFile.nextDouble();
               numOfCharges++;
            }
        }
    }
    finally
    {
        openFile.close();
    }

   double averageHours = (totalHours/numOfHourInputs);
   double averagePaid = (totalPaid/numOfCharges);

   System.out.println("Total hours: " + totalHours);
   System.out.println("Num hours input: " + numOfHourInputs);
   System.out.println("----------------------------------------");
   System.out.println("Average hours: " + averageHours);
   System.out.println("");
   System.out.println("Total payments: " + totalPaid);
   System.out.println("Num payments input: " + numOfCharges);
   System.out.println("----------------------------------------");
   System.out.println("Average paid: " + averagePaid);
}

以下是我得到的输出:

Total hours: 120.0
Num hours input: 5
----------------------------------------
Average hours: 24.0

Total payments: 77.75
Num payments input: 5
----------------------------------------
Average paid: 15.55