gpt4 book ai didi

java - 货币单位换算

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:19 25 4
gpt4 key购买 nike

我有美元可以工作,但现在我不知道如何正确显示 25 美分、10 美分、5 美分和 10 分。从技术上讲,它们会出现,但我的教授希望 1.35 美元显示为 1 美元 1 25 美分 1 毛钱。但是,我的显示为 1 美元 5 夸特 13 角硬币 27 镍币 135 便士。这是我的代码:

import java.util.Scanner;

public class ComputeChange {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an amount in double, for example 11.56: " );
double number = input.nextDouble();
System.out.println("Your amount " + number + " consists of ");
int remainingamount = (int)(number*100);
int Quarters = remainingamount/25;
int Dimes = remainingamount/10;
int Nickels = remainingamount/5;
int Pennies = remainingamount;

if (number == 1) {
System.out.print("1 dollar ");
}
else if (number > 1) {
System.out.print((int)number + " dollars ");
}
if (number == 0) {
System.out.println("");
}
System.out.print(Quarters + " Quarters " + Dimes + " Dimes " + Nickels + " Nickels " + Pennies + " Pennies");
}
}

最佳答案

对于您计算的每种不同的硬币,您需要将其从剩余金额中删除。如(未经测试):

    int Dollars = (int)number;
int remainingamount = (int)((number-Dollars)*100);
int Quarters = remainingamount/25;
remainingamount -= Quaters * 25;
int Dimes = remainingamount/10;
remainingamount -= Dimes * 10
int Nickels = remainingamount/5;
remainingamount -= Nickels * 5
int Pennies = remainingamount;

关于java - 货币单位换算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28624396/

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