繁体   English   中英

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

[英]java: loop produces incorrect output when reading from file?

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

         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

删除其他:

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

以下代码

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();

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM