即使数字太大,它仍在处理中(忽略,我修复了它)

Even though the numbers too large, it's still being processed (ignore, I fixed it)

import java.io.*;

导入java.util.*;

public class 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

您正在处理浮点算术错误。 如果你打印出 value * 100,你会发现它是 7898.9999... 浮点算术错误在发生时只有很小的一部分。

针对您的情况的简单解决方案是添加 .1,然后转换为 int。

更改此行:

remainingAmount = (int)(value * 100);

至:

remainingAmount = (int)(value * 100 + .1);

打破这一点的唯一方法是测试文件包含使用超过 2 位小数的值。但是既然你在和钱打交道,你不应该为此担心。

我不会写代码本身。我猜这是手工制作的例子。调试此类问题的一般经验法则是:查看“最后”或“最开始”。 该部门工作正常,因此它必须在一开始就发生,这就是您的价值被转换为 int 的地方。如果您检查值 * 100 的乘积结果,那么您会看到 7898.999999999999,而不是您预期的 7899。您应该使用 Math.ceil 来获得正确的值,如下所示:

 int result = (int)Math.ceil(value * 100);