Java 如何将一个数字分成若干部分,使其在……范围内。。。。。!
我有一个电话号码15000Java 如何将一个数字分成若干部分,使其在……范围内。。。。。!,java,Java,我有一个电话号码15000 对于0-5000我不必申请任何折扣 对于5001-10000我必须申请10%折扣(5000+4500=9500) 对于10000以上的10000我必须申请20%的折扣(5000+4500+4000=13500) 我们如何划分一个数字,使其落在特定范围内?可以这样做: int amount = ... int total = 0; if (amount > 10_000) { // Apply 20 % on the amount above 10 K
0-5000
我不必申请任何折扣5001-10000
我必须申请10%
折扣(5000+4500=9500
)10000
我必须申请20%
的折扣(5000+4500+4000=13500
)我们如何划分一个数字,使其落在特定范围内?可以这样做:
int amount = ...
int total = 0;
if (amount > 10_000) {
// Apply 20 % on the amount above 10 K
total += (amount - 10_000) * 0.8d;
amount = 10_000;
}
if (amount > 5_000) {
// Apply 10 % on the amount above 5 K
total += (amount - 5_000) * 0.9d;
amount = 5_000;
}
// Apply 0 % on the rest
total += amount;
我不知道在5000英镑上打10%的折扣怎么会使它变成9500英镑。@DarshanMehta这就像美国的税括号一样,你全额支付第一笔
5000英镑
,然后在第二笔5000英镑
上打10%的折扣,所以你只需支付4500英镑
。然后,欠款总额为9500。将其更正为13500如何知道为什么在这个问题上加上java标记;或者你的问题到底是什么…当然。如果我不是这样的自我主义者,我会和他分享我在过去3分钟里解决问题的代码。