【问题标题】:Java addition subtraction with moneyJava用钱加减法
【发布时间】:2015-01-13 19:49:12
【问题描述】:

我正在尝试加减美元和美分,但在超过 100 美分和低于 0 美分时遇到了麻烦。我的代码可以很好地添加任何东西,直到我需要将 100 美分转换为一美元。我无法将我的话转化为代码,但我了解将美分转换为美元需要做什么。

仅供参考,这是一个类,所以我有静态方法加法/减法和类方法加法/减法的代码

我的代码:

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;
    }
}

类代码:

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;
    }
}

【问题讨论】:

  • 如果是我,我可以考虑不同时拥有两个 int 美元;整数分;相反,只有 int 美分;那么 10.55 美元就是 1055 美分。这样算术很简单。您可以使用通常的算术运算符。
  • 将一个数字除以 100 (/) 可以得到确切的美元金额,而除以 100 (%) 将得到剩余的美分数。
  • 这两个 cmets 都是正确的——你想使用“标量”数学,使用一个缩放的值来跟踪,这样它就可以保存你所有的总和。然后,您可以使用除法和 mod 转换为您想要的输出格式。
  • 以上三个 cmets 都是正确的,但请记住,有时您可能会得到负值,并且使用整数除法和 mod 会显示 -$10.60 为 -$11 和 +40 美分。

标签: java class netbeans static addition


【解决方案1】:

请考虑这个:

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());
    }
}

【讨论】:

  • 这是最好的方法。干净且没有多余的数据存储(例如每一美元和美分一个变量)。
【解决方案2】:

我会写一个 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减美元=money.dollars-money2.dollars; int subcents=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;
}  

【讨论】:

    【解决方案3】:

    我更新了你的金钱等级来解决这个问题。

    加钱

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

    this.dollars = dollar+cent / 100;
    

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

    this.cents = cents % 100
    

    这样一来,美分将始终在 0-99 之间。

    金钱减法

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

    更新货币类

    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;
        }
    }
    

    【讨论】:

    • 将单个值(金额)保存在 2 个单独的变量中并不是一个好主意,尤其是当值的范围有限时。如果要强调两种不同的含义,请提供 getCents() 之类的 getter 方法,并在 getter 内部执行计算。
    • @f1sh 这确实是最好的解决方案。但我不想完全弄乱 OP 的代码 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多