gpt4 book ai didi

java - 尽管数字太大,但仍在处理中(忽略,我已修复)

转载 作者:行者123 更新时间:2023-12-01 11:55:23 25 4
gpt4 key购买 nike

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 个 25 美分、0 个一角、0 个镍币和 0 个便士

9755.35您的 9755.35 美元金额包括:39021 25 美分、1 角硬币、0 镍币和 0 便士

875.4您的 875.4 美元金额包括:3501 25 美分、1 角硬币、1 镍币和 0 便士

78.99您的 78.99 美元金额包括:315 25 美分、2 角硬币、0 镍币和 4 便士

5.0您的 5.0 美元金额包括:20 个 25 美分、0 个 10 分、0 个 5 分币和 0 个便士

0.7您的 0.7 美元金额包括:2 个 25 美分、2 个 10 分、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

最佳答案

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

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

关于java - 尽管数字太大,但仍在处理中(忽略,我已修复),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28493800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com