java:loop在读取文件时产生不正确的输出?
此循环生成小时数和支付金额的平均值,但输出在数学上不正确。如何编辑此代码以生成正确的平均工时值和平均薪资值java:loop在读取文件时产生不正确的输出?,java,file,while-loop,average,scanning,Java,File,While Loop,Average,Scanning,此循环生成小时数和支付金额的平均值,但输出在数学上不正确。如何编辑此代码以生成正确的平均工时值和平均薪资值 Scanner openFile = new Scanner(inFile); while (openFile.hasNext()) { if (openFile.hasNextDouble()) { totalHours += openFile.nextDouble();
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.09.95
b 10.0 13.95
b 20.0 13.95
c 50.0 19.95
c 30.0 19.95
删除else:
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
如果用户可以向文件中添加更多数据,该怎么办?所以,
双[][]值
可能会改变吗?如果文件发生更改,我如何从中读取更多值@Smutje请咨询互联网,了解如何将文件读入您喜欢的任意数据结构,例如double数组。这类问题有无数的例子!跳过第一个标记是什么意思?我怎么能做到呢@Michaelth您的示例中的第一个标记是a/b/c,因此openFile.next()只是在中读取该标记并移动到下一个标记,而不对该值做任何处理。如何让程序计算文件中所有值的平均值,而不是跳过值@michaelThis不是跳过值,而是跳过非值字段。您的示例中有一行列为“a 10.0 9.95”,因此第一个openFile.next()语句跳过了“a”。如果您的行中实际上没有“a”,请去掉调用openFile.next()的行;我已经在上面添加了输出。