嗯..这是给我的指令:如何在java中抛出异常?
支持另外&减法与其他货币对象
抛出异常,如果货币是不相容的。
嗯..我已经有了add()的钱课。在这种情况下,我如何抛出异常?我的意思是,我知道该怎么做,但是我应该在哪里做?我是否在同一个班级上完成课程?还是应该在其他所有东西发生的main()中的某处执行异常抛出?
public class Money {
Currency currency;
int dollar;
int cents;
//constructor
public Money(Currency currency, int dollar, int cents) {
super();
this.currency = currency;
this.dollar = dollar;
this.cents = cents;
}
.
.
.
.
public Money add(Money moneyToBeAdded){
Money result = new Money(moneyToBeAdded.getCurrency(),0,0);
Money totalInCents;
if(compareCurrency(moneyToBeAdded)){
totalInCents = new Money(moneyToBeAdded.getCurrency(),0,(moneyToBeAdded.toCents() + cents));
result = totalInCents.toDollars();
}//to do convert currency
return result;
}
public Money subtract()
public boolean compareCurrency(Money money){
return money.currency.equals(currency);
}
}
有很多可用的资源可以通过[简单的谷歌搜索]找到(https://www.google.com/search?q=how+to+throw+an+exception+in+java ),没有人帮助你? –
我想我只是不确定如何去做。 :)来吧,不要这么苛刻。 –