Java用钱做加减法

Java addition subtraction with money

我正在尝试加减美元和美分,但在超过 100 美分和低于 0 美分时遇到问题。在我需要将 100 美分转换为 1 美元之前,我的代码可以很好地添加任何内容。我无法将我的话转化为代码,但我明白需要做什么才能将美分转换为美元。

仅供参考,这是针对 class 的,所以这就是为什么我有静态方法 addition/subtraction 和 class 方法 addition/subtraction

的代码

我的代码:

package moneyapp;

public class MoneyApp {

    public static void main(String[] args)
    {
        Money money1=new Money(99,99);
        Money money6=new Money(100,00);
        Money money7=new Money(0,1);

        add(money1,money7);
        System.out.println("The sum of "+money1+" and "+money7+" is "+money1.add(money7));
        subtract(money6,money7);
        System.out.println("The difference of "+money6+" and "+money7+" is "+money6.subtract(money7));
    }

    static Money add(Money money, Money money2)
    {
        int adddollars=money.dollars+money2.dollars;
        int addcents=money.cents+money2.cents;
        Money addmoney=new Money(adddollars,addcents);
        System.out.println(addmoney.toString());
        return addmoney;
    }
    static Money subtract(Money money, Money money2)
    {
        int subtractdollars=money.dollars-money2.dollars;
        int subtractcents=money.cents-money2.cents;
        Money subtractmoney=new Money(subtractdollars,subtractcents);
        System.out.println(subtractmoney.toString());
        return subtractmoney;
    }
}

Class代码:

package moneyapp;

public class Money
{
    int dollars;
    int cents;

    public Money()
    {
        dollars=0;
        cents=0;
    }

    public Money(int dollar, int cent)
    {
        dollars=dollar;
        cents=cent;
    }

    public Money(int dollar)
    {
        dollars=dollar;
        cents=00;
    }

    public String toString()
    {
        if(cents<10)
        {
            return "$"+dollars+"."+"0"+cents;
        }
        else
        {
            return "$"+dollars+"."+cents;
        }
    }

    public int getDollars()
    {
        return dollars;
    }

    public int getCents()
    {
        return cents;
    }

    public void setDollars(int dollars)
    {
        this.dollars=dollars;
    }

    public void setCents(int cents)
    {
        this.cents=cents;
    }

    public Money add(Money other)
    {
        int dol=dollars+other.dollars;
        int cen=cents+other.cents;
        Money answer=new Money(dol,cen);
        return answer;
    }
    public Money subtract(Money other)
    {
        int dol=dollars-other.dollars;
        int cen=cents-other.cents;
        Money answer=new Money(dol,cen);
        return answer;
    }
}

我更新了你的钱 class 来解决这个问题。

加钱

100 美分等于 1 美元,因此您可以将美元初始化为:

this.dollars = dollar+cent / 100;

分的数量等于除以 100 的余数

this.cents = cents % 100

这样一来,分就会一直在0-99之间。

减钱

当您的美分金额小于零时,您可以通过减少美元金额来借到 100 美分。

更新货币Class

public class Money {
    public final int dollars;
    public final int cents;

    public Money() {
        dollars = 0;
        cents = 0;
    }

    public Money(int dollar, int cent) {
        dollars = dollar + cent / 100;
        cents = cent % 100;
    }

    public Money(int dollar) {
        dollars = dollar;
        cents = 0;
    }

    public String toString() {
        return String.format("$%d.%02d", dollars, cents);
    }

    public Money add(Money other) {
        int dol = dollars + other.dollars;
        int cen = cents + other.cents;
        Money answer = new Money(dol, cen);
        return answer;
    }

    public Money subtract(Money other) {
        int dol = dollars - other.dollars;
        int cen = cents - other.cents;

        while (cents < 0) {
            --dol;
            cen += 100;
        }
        Money answer = new Money(dol, cen);
        return answer;
    }
}

我会放一个 if 语句说如果美分 >= 100 然后添加到美元 静态货币添加(货币货币,货币货币2) { int adddollars=money.dollars+money2.dollars; int addcents=money.cents+money2.cents;

    if(addcents >= 100){
        adddollars++;
    }

    Money addmoney=new Money(adddollars,addcents);
    System.out.println(addmoney.toString());
    return addmoney;
}

至于你的减法方法,我会把类似 静态货币减法(货币货币,货币货币 2) { int subtractdollars=money.dollars-money2.dollars; int subtractcents=money.cents-money2.cents;

    if(subtractcents < 0)
        money.dollars--;
    money.cents=money.cents + 100;
    subtractcents= money.cents-money2.cents;


    Money subtractmoney=new Money(subtractdollars,subtractcents);
    System.out.println(subtractmoney.toString());
    return subtractmoney;
}  

改为考虑这个:

public class Money {

    private int m;

    public Money(int m) {
        this.m = m;
    }

    public int getDollars() {
        return m / 100;
    }

    public int getCents() {
        return m % 100;
    }

    public int get() {
        return m;
    }

    public Money add(Money other) {
        return new Money(m + other.get());
    }

    public Money subtract(Money other) {
        return new Money(m - other.get());
    }
}