//试着写了一个~~
///: Filename: ForeignCurrency.java
public class ForeignCurrency {
/**外币兑本币,ca 要兑换的外币金额*/
public double f2RMB(Currency src,double ca){
return ca*src.getExchangeRate();
}
/**本币兑外币,RMB 要兑换的本币金额*/
public double RMB2F(Currency dest,double RMB){
return RMB/dest.getExchangeRate();
}
/**外币兑外币,cash 要兑换的原外币(src)金额*/
public double f2F(Currency src,Currency dest,double cash){
return src.getExchangeRate()/dest.getExchangeRate()*cash;
}
public static void main(String[] args){
ForeignCurrency x = new ForeignCurrency();
Currency rmb = new Currency(CurType.本币,1);//定义一个RMB类,兑RMB的汇率是 1
Currency us$ = new Currency(CurType.美元,7.3);//定义一个美金类,兑RMB的汇率是 7.3
Currency au$ = new Currency(CurType.澳元,9.65);//定义一个澳元类,兑RMB的汇率是 9.65
Currency jp$ = new Currency(CurType.日元,0.0101);//定义一个日元类,兑RMB的汇率是 0.0101
double RMB = 888;
double US$ = 123;
double AU$ = 234;
double JP$ = 5678.9;
//美元兑换RMB;
double us2rmb = x.f2RMB(us$, US$);
System.out.println(us$.getCURName()+": "+US$+" -> "+rmb.getCURName()+": "+us2rmb+" 汇率:"+us$.getExchangeRate()/rmb.getExchangeRate());
//澳元兑换RMB
double au2rmb = x.f2RMB(au$, AU$);
System.out.println(au$.getCURName()+": "+AU$+" -> "+rmb.getCURName()+": "+au2rmb+" 汇率:"+au$.getExchangeRate()/rmb.getExchangeRate());
//日元兑换RMB
double jp2rmb = x.f2RMB(jp$, JP$);
System.out.println(jp$.getCURName()+": "+JP$+" -> "+rmb.getCURName()+": "+jp2rmb+" 汇率:"+jp$.getExchangeRate()/rmb.getExchangeRate());
//美元兑换澳元
double us2au = x.f2F(us$, au$,US$);
System.out.println(us$.getCURName()+": "+US$+" -> "+au$.getCURName()+": "+us2au+" 汇率:"+us$.getExchangeRate()/au$.getExchangeRate());
//....................
//RMB兑换澳元
double rmb2au = x.RMB2F(au$,RMB);
System.out.println(rmb.getCURName()+": "+RMB+" -> "+au$.getCURName()+": "+rmb2au+" 汇率:"+rmb.getExchangeRate()/au$.getExchangeRate());
//....................
}
}
//货币名称枚举
enum CurType{
本币,美元,澳元,日元,法郎//分别指:人民币,美金,澳元,日元,法郎...
}
//货币类,所有汇率都是以RMB为基准的
class Currency{
private double xr;//对RMB的汇率
private CurType type;//名称,比如US$
/**参数 CUR 货币名称,double 对RMB的汇率*/
public Currency(CurType c,double xRMB){
xr = xRMB;
type = c;
}
/**获取外币对RMB的汇率*/
public double getExchangeRate() {
return xr;
}
/**获取货币名称*/
public String getCURName(){return type.name();}
}
主要知道汇率就行了~~
方法应该不难吧,
用Scanner得到输入,再将输入数据根据汇率转换,再输出结果
莫非我理解错了
呵呵~我初学JAVA~~说错了别喷我哈~