超简单,帮忙将以下JavaScript代码实现的功能转换为java/**
 * currency1 and currency2 are string like 'EUR', 'USD' 
 */
function getExchangeRate(currency1, currency2) {
var a = {EUR:10, USD:8, JPY: 2, AUD: 3};
return a[currency1]/a[currency2];
}
实现为:
public static Double getExchangeRate(String currency1, String currency2){}

解决方案 »

  1.   

    public static Double getExchangeRate(String currency1, String currency2){
       Object[][] a = {{"EUR",10},{"USD","8"},{"JPY","2"},{"AUD","3"}},
      return a[currency1][1]/a[currency2][1]
    }
      

  2.   

    这样?public static Double getExchangeRate(String currency1, String currency2){
        int []a = {10,8,2,3};
        return ((double)a[Integer.parseInt(currency1)])/a[Integer.parseInt(currency2)];
    }
      

  3.   

    看来Java麻烦一些 Map<String, Double> currencyList = new HashMap<String, Double>();
    currencyList.put("EUR", new Double(10));
    currencyList.put("USD", new Double(8));
    currencyList.put("JPY", new Double(2));
    currencyList.put("AUD", new Double(3));
    return currencyList.get(currency1).doubleValue() / currencyList.get(currency2);
      

  4.   

    看这个 刚才的不对
    public static Double getExchangeRate(String currency1, String currency2){
       Map<String,Integer> map = new HashMap<String,Integer>();
       map.put("EUR",10);
       map.put("USD",8);
       map.put("JPY",2);
       map.put("AUD",3);
      return Double.valueOf(map.get(currency1))/Double.valueOf(map.get(currency2));
    }
      

  5.   

    完整的代码 public static void main(String[] args) {
    /** * currency1 and currency2 are string like 'EUR', 'USD' */ 
    System.out.println(getExchangeRate("EUR", "JPY"));
    }
    public static Double getExchangeRate(String currency1, String currency2){
    Map<String, Double> currencyList = new HashMap<String, Double>();
    currencyList.put("EUR", new Double(10));
    currencyList.put("USD", new Double(8));
    currencyList.put("JPY", new Double(2));
    currencyList.put("AUD", new Double(3));
    return currencyList.get(currency1).doubleValue() / currencyList.get(currency2);
    }