本文介绍了用钱减去Java加法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加减美元和美分,但是我无法超过100美分且低于0美分.我的代码可以很好地添加任何东西,直到我需要将100美分转换为1美元为止.我在将自己的文字输入代码时遇到了麻烦,但是我知道将美分转换为美元需要做什么.

I am trying to add and subtract dollars and cents but I am having trouble with going over 100 cents and under 0 cents. My code works fine for adding anything until I need to convert 100 cents into a dollar. I'm having trouble putting my words into code, but I understand what needs to be done to convert cents into a dollar.

仅供参考,这就是为什么要使用一类,这就是为什么我有用于静态方法加/减和类方法加/减的代码的原因

FYI this is for a class so that is why I have code for static method addition/subtraction and class method 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;
    }
}

类代码:

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

推荐答案

请考虑以下内容:

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

这篇关于用钱减去Java加法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 21:37