import java.io.*;
import java.util。*;
公共课CH04 {
// Constants Declaration Section
//******************************
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
// Variables Declaration Section
//******************************
int quarters;
int dimes;
int nickles;
int numberOfQuarters;
int numberOfDimes;
int remainingAmount;
int numberOfNickles;
int numberOfPennies;
int maxSize;
// Variables Initialization Section
//*********************************
Scanner data = new Scanner(new File("C:\\testing3.txt")); //Reads from text file
ArrayList<Double> datadata = new ArrayList<Double>(); //Creates Array
quarters = 25;
dimes = 10;
nickles = 5;
maxSize = 10000;
// Code Section
//*************
while(data.hasNextDouble()) //Retrieves data from text file and places it in array
{
datadata.add(data.nextDouble()); //Retrieves data from text file and places it in array
}
for (int i = 0; i < datadata.size(); i++) //Loops through the data
{
double value = datadata.get(i); //returns number found in file as a double 'value' then runs it through the calculator
System.out.println(value);
while(value > maxSize) //If value is greater than 10,000, it will be skipped and not read through the calculator
{
}
remainingAmount = (int)Math.ceil((value) * 100); //multiplies value by 100
numberOfQuarters = remainingAmount / quarters; //divides value by 25
remainingAmount = remainingAmount % quarters; //returns the remaining value
numberOfDimes = remainingAmount / dimes; //divides the remaining value by 10
remainingAmount = remainingAmount % dimes; //returns the remaining value of that
numberOfNickles = remainingAmount / nickles; //divides the remaining value from dimes by 5
remainingAmount = remainingAmount % nickles; //returns remaining value
numberOfPennies = remainingAmount; //divides remaining value by 1
System.out.print("Your amount of $" + value + " consists of: \n" + numberOfQuarters + //prints out the amount of quarters, dimes, nickels, pennies
" quarteres, " + numberOfDimes + " dimes, " + numberOfNickles + " nickles, and " + //that make up each value found inside the file.
numberOfPennies + " pennies");
System.out.print("\n\n");
}
//Output Section
//**************
//Resource Cleaning
//*****************
data.close();
}
}
**Output:
10001.0 值太大,无法处理10001.0 您的$ 10001.0金额包括: 40004夸脱,0角钱,0镍,0便士
9755.35 您的9755.35美元金额包括: 39021个四分之一,1个硬币,0个镍币和0个便士
875.4 您的$ 875.4金额包括: 3501夸脱,1角钱,1个镍币和0便士
78.99 您的78.99美元包括: 315夸脱,2角钱,0个镍币和4个便士
5.0 您的5.0美元金额包括: 20个四分之一,0角钱,0个镍币和0个便士
0.7 您的0.7美元包括: 2个四分之一,2个角钱,0个镍币和0个便士
使用的值:[10001.0,9755.35,875.4,78.99,5.0,0.7] **
出于某种原因,我输出的斜体部分收到1便士。这让我感到困惑,因为输出的其余部分是正确的。
我的文本值中的值是:
10,001.00 -
9,755.35 -
875.40 -
78.99 -
5.00 -
0.70
答案 0 :(得分:1)
我不会写代码本身。我猜这是手工制作的一个例子。调试此类问题的一般经验法则是:查看最后或最开始的'。 除法是正确的,因此它必须在一开始就发生,这是你的值被转换为int的地方。如果你检查产品值* 100的结果,那么你会看到7898.999999999999,而不是你想象的7899。您应该使用Math.ceil来获取正确的值,如下所示:
int result = (int)Math.ceil(value * 100);
答案 1 :(得分:0)
您正在处理浮点算术错误。
如果你打印出value * 100
,你会发现它出现在7898.9999 ......
浮点运算误差仅在发生时非常小。
您案例的简单解决方案是添加.1
然后转换为int。
更改此行:
remainingAmount = (int)(value * 100);
为:
remainingAmount = (int)(value * 100 + .1);
如果测试文件包含的值超过2个小数位,则打破此问题的唯一方法就是这样。但既然你正在处理金钱问题,你就不必担心。