gpt4 book ai didi

java - 获取任意值(value)的纸币和硬币的数量

转载 作者:行者123 更新时间:2023-12-01 10:03:23 27 4
gpt4 key购买 nike

我想制作一个小系统,可以为我返回任何值(value)的最佳纸币和硬币数量。

这是我的代码:

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
double amount = Double.parseDouble(br.readLine());
if (amount > 0 && amount < 1000000.00) {
// ############################# BILLS ############################
double rest100 = amount / 100;
double rest50 = amount % 100;
double rest20 = rest50 % 50;
double rest10 = rest20 % 20;
double rest5 = rest10 % 10;
double rest2 = rest5 % 5;

// ############################ COINS ############################
double rest01 = rest2 % 2;
double rest050 = rest01 % 1;
double rest025 = rest050 % .5;
double rest010 = rest025 % 25;
double rest005 = rest010 % .1;
double rest001 = rest005 % .05;

System.out.println("BILLS:\n"
+ (int) rest100
+ " bill(s) of 100.00\n"
+ (int) rest50 / 50
+ " bill(s) of 50.00\n"
+ (int) rest20 / 20
+ " bill(s) of 20.00\n"
+ (int) rest10 / 10
+ " bill(s) of 10.00\n"
+ (int) rest5 / 5
+ " bill(s) of 5.00\n"
+ (int) rest2 / 2
+ " bill(s) of 2.00\n"
+ "COINS:\n"
+ (int) (rest01 / 1)
+ " coin(s) of 1.00\n"
+ (int) (rest050 / .5)
+ " coin(s) of 0.50\n"
+ (int) (rest025 / .25)
+ " coin(s) of 0.25\n"
+ (int) (rest010 / .1)
+ " coin(s) of 0.10\n"
+ (int) (rest005 / .05)
+ " coin(s) of 0.05\n"
+ (int) (rest001 / .01)
+ " coin(s) of 0.01");
}
}

嗯,这几乎是正确的,钞票运行完美,我的问题是硬币。

以下是一些输入:

  • 576.73//正确打印
  • 8.45//打印不正确
  • 9.45//打印错误,看下面:

实际输出:

BILLS:
0 bill(s) of 100.00
0 bill(s) of 50.00
0 bill(s) of 20.00
0 bill(s) of 10.00
1 bill(s) of 5.00
2 bill(s) of 2.00
COINS:
0 coin(s) of 1.00
0 coin(s) of 0.50
1 coin(s) of 0.25
4 coin(s) of 0.10
0 coin(s) of 0.05
4 coin(s) of 0.01

预期输出:

BILLS:
0 bill(s) of 100.00
0 bill(s) of 50.00
0 bill(s) of 20.00
0 bill(s) of 10.00
1 bill(s) of 5.00
2 bill(s) of 2.00
COINS:
0 coin(s) of 1.00
0 coin(s) of 0.50
1 coin(s) of 0.25
2 coin(s) of 0.10
0 coin(s) of 0.05
0 coin(s) of 0.01

PS:我不会发布所有预期的输出,因为这会让问题比现在更大,但如果你需要,我可以发布。提前致谢。

最佳答案

只需乘以 100 并以美分为单位进行计算即可。

关于java - 获取任意值(value)的纸币和硬币的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36637177/

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