2017-07-26 81 views
4
package com.operators; 

    import java.util.ArrayList; 
    import java.util.HashMap; 
    import java.util.List; 
    import java.util.Map; 
    import java.util.Scanner; 
    import java.util.function.BinaryOperator; 

    public class TotalCost { 

     public static void main(String[] args) { 
      Scanner scan = new Scanner(System.in); 
      double mealCost = scan.nextDouble(); // original meal price 
      int tipPercent = scan.nextInt(); // tip percentage 
      int taxPercent = scan.nextInt(); // tax percentage 
      scan.close(); 

      Map<Double,Double> map = new HashMap<>(); 
      map.put(mealCost, (double)tipPercent); 
      map.put(mealCost, (double)taxPercent); 

      BinaryOperator<Double> opPercent = (t1,t2) -> (t1*t2)/100; 
      BinaryOperator<Double> opSum = (t1,t2) -> (t1+t2); 
      calculation(opPercent,map); 
     } 

     public static void calculation(BinaryOperator<Double> opPercent , Map<Double,Double> map) { 
      List<Double> biList = new ArrayList<>(); 
      map.forEach((s1,s2)-> biList.add(opPercent.apply(s1, s2))); 
     } 
    } 
  1. 我有我試圖在Java中8解決使用BinaryOperator.There被三個輸入到本申請[mealCost(雙),tipPercent(INT)的以下問題,taxPercent (INT)。
  2. 我試圖計算出以下值:加成在Java中使用8 BinaryOperator

    tip = (mealCost*tipPercent)/100; 
    tax = (mealCost*taxPercent)/100; 
    TotalCost = mealCost+tip +tax; 
    
  3. 我無法整數輸入傳遞到BinaryOperator的應用方法。此外,計算的值biList是不適當的。 以下是我的代碼

+0

您可能還需要使用'DoubleBinaryOperator'其使用,而不是'Double'包裝的原始值 – Lino

回答

0

您在Map中放置兩次相同的密鑰,因此第二個值會覆蓋第一個值。我不認爲Map適合這些計算。您可以改用List<SomePairType>

或者保持一個變量和其他值mealCostList<Double>

public static void calculation(BinaryOperator<Double> opPercent, Double cost, List<Double> rates) { 
     List<Double> biList = new ArrayList<>(); 
     rates.forEach(d-> biList.add(opPercent.apply(cost, d))); 
    } 
0

你把這些值在地圖相同的密鑰。所以最初的價值被新價值所覆蓋。

嘗試以下

package com.operators; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Scanner; 
import java.util.function.BinaryOperator; 

public class TotalCost { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     double mealCost = scan.nextDouble(); // original meal price 
     int tipPercent = scan.nextInt(); // tip percentage 
     int taxPercent = scan.nextInt(); // tax percentage 
     scan.close(); 

     Map<Double,Double> map = new HashMap<>(); 

     map.put(mealCost, (double)taxPercent + (double)tipPercent); 

     BinaryOperator<Double> opPercent = (t1,t2) -> (t1*t2)/100; 
     BinaryOperator<Double> opSum = (t1,t2) -> (t1+t2); 
     calculation(opPercent,map); 
    } 

    public static void calculation(BinaryOperator<Double> opPercent , Map<Double,Double> map) { 
     List<Double> biList = new ArrayList<>(); 
     map.forEach((s1,s2)-> biList.add(opPercent.apply(s1, s2))); 
    } 
} 
2

代碼中的其他答案已經告訴你錯誤的根本原因在計算中。關於你問題的第二部分:

我無法整數關口輸入的 apply方法BinaryOperator

這是因爲你已經宣佈你BinaryOperator需要Double作爲輸入參數類型和返回類型。 BinaryOperator只有一個類型作爲參數,它既是輸入參數的類型,也是返回類型,所以如果您有Double作爲方法參數,並且Double也作爲返回類型,您可以決定使用BinaryOperator。如果您有多個類型作爲參數和返回類型,則可以考慮使用BiFunction

BiFunction<Double, Integer, Double> opPercent = (t1,t2) -> (t1*t2)/100; 

這裏我們說的輸入參數是雙和對應mealCost和taxPercent INETGER和返回類型是雙。

你甚至可以下面的例子中有更多數量的參數定義自己的功能接口,如:

public class TotalCost { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     double mealCost = scan.nextDouble(); // original meal price 
     int tipPercent = scan.nextInt(); // tip percentage 
     int taxPercent = scan.nextInt(); // tax percentage 
     scan.close(); 

     TriFunction<Double, Integer, Integer, Double> calcCost = (cost, tipPct, taxPcnt) -> 
             (cost + (cost * tipPct/100) + (cost * taxPcnt/100)); 
     Double totalBill = calculation(calcCost, mealCost, tipPercent, taxPercent); 
     System.out.println(totalBill); 
    } 

    public static Double calculation(TriFunction<Double, Integer, Integer, Double> calcCost , 
             Double mealCost, Integer tipPct, Integer taxPct) { 
     return calcCost.apply(mealCost, tipPct, taxPct); 
     } 
    } 

    @FunctionalInterface 
    interface TriFunction<T,U,V,R> { 
     R apply(T t, U u, V v); 
    }