First I would remove the println inside the function and make it a double returning function.
Maybe lambdas to define short methods is what you are looking for. Here we can use DoubleBinaryOperator.
public static void main(String[] args) {
DoubleBinaryOperator add = (x, y) -> x + y;
DoubleBinaryOperator sub = (x, y) -> x - y;
DoubleBinaryOperator mul = (x, y) -> x * y;
DoubleBinaryOperator div = (x, y) -> x / y;
double num1 = 5;
double num2 = 10;
System.out.printf("sum %f%nsub %f%nmul %f%ndiv %f%n",
add.applyAsDouble(num1, num2),
sub.applyAsDouble(num1 + num2, num1),
mul.applyAsDouble(num1, num2),
div.applyAsDouble(num1 + num2, (num1 + num2) - num1));
}
Unfortunately .applyAsDouble is a bit awkward. applyAsDouble should be interpreted as: apply two (double) arguments and get the result as double value.
发布评论
评论(3)
首先,我将删除功能中的println,并使其成为双重返回功能。
也许您要寻找的是定义简短方法的lambdas。
在这里,我们可以使用
doubleBinaryOperator
。不幸的是
.applyasdouble
有点尴尬。applyasdouble
应解释为: apply 两个(double)参数,并将结果作为double 值。您的解决方案也很好。
First I would remove the println inside the function and make it a double returning function.
Maybe lambdas to define short methods is what you are looking for.
Here we can use
DoubleBinaryOperator
.Unfortunately
.applyAsDouble
is a bit awkward.applyAsDouble
should be interpreted as: apply two (double) arguments and get the result as double value.Your solution is fine too.
您可以这样写。 :)
you could write it like this. :)