2017-07-26 84 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); 
    }