public class CalculateWithoutOperator { public static void main(String[] args) { int n=99; int result=calculate(n); System.out.print(result); } static int calculate(int n){ int result=0; boolean flag=false; //No.1 //开始写代码,不用操作符计算1+2+……+n //end_code }}
4 回答
已采纳
_潇潇暮雨
TA贡献646条经验 获得超225个赞
用了java 8的reduce
import java.util.Arrays; import java.util.List; import java.util.Optional; public class Sample { public static void main(String[] args){ final List<Integer> numbers = Arrays.asList(1, 2, 3, 4); final Optional<Integer> sum = numbers.stream() .reduce((a, b) -> a + b); sum.orElseGet(() -> 0); System.out.println(sum.get()); } }
添加回答
举报
0/150
提交
取消