我有学习功能接口。我写了下面的代码返回一个 Consumer
从 Function
接口,但它不工作。它正在返回输出 0
. 我不明白为什么它会回来 0
.
代码:
public static void main(String[] args) {
Function<Integer, Integer> factorial = n -> IntStream.rangeClosed(2, n)
.reduce(1, (x, y) -> x * y);
Function<Integer, Consumer<Integer>> f3 = n -> {
return x -> System.out.println(factorial.apply(x * factorial.apply(n)));
};
f3.apply(5).accept(2); // output 0
}
有人能解释这是为什么吗( f3.apply(5).accept(2)
)返回 0
. 有没有其他方法来实现这一点。
2条答案
按热度按时间pdtvr36n1#
为了得到
Consumer
在变量中,需要将代码分成两部分从这里你会看到一些东西是找不到的,就像你的消费者会做的那样
(x * 5!)!
哪个是(120x)!
所以用2
->240!
关于10^468
,其中整数最多只能容纳2^32
我建议你去掉一层factorial
为了得到更容易理解的结果vx6bjr1n2#