Closed. This question needs debugging details。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
导入java.util。*;
公共课程CH04 {
}
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便士。这让我感到困惑,因为其余的输出是正确的。
我的文本值内的值是:
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
import java.io.*;
导入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
最佳答案
我不会写代码本身。我想以手工为例。调试此类问题的一般经验法则是:从头到尾或从头开始。
除法工作正常,因此必须在一开始就发生,这就是将您的值转换为整数的地方。如果您检查值* 100的乘积结果,那么您会看到7898.999999999999,而不是您期望的7899。您应该使用Math.ceil来获得正确的值,如下所示:
int result = (int)Math.ceil(value * 100);
关于java - 即使数字太大,它仍在处理中(忽略,我已修复它),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28493800/
10-10 18:25