用java代码实现:求 1+..n不能使用乘除法、 for while if else switch case 等关键字以及条件判断语句

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

}

}


qq_守护中的那个她_03928733
浏览 2474回答 4
4回答

_潇潇暮雨

用了java 8的reduceimport 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());     } }

慕数据8267187

TYLPWGNNBFEIGXDQUQXNAYBPAMZPSJPFPTKUCAHEUKQBCEUNIXUDJGNWQQDNJHXULBSYZTMWVTJCAANMPISZKGWMSSSCVEXUSUUKBHQLIPMZHJEELLBEJGWRKVBOYFBEYSBBYQEEDHNMNVYBUJCZTWCOEHKMEJZWWPMSJAJWEQNTKGXQHHUMBTGTTATMMLDBMZUUKDAJMAKBGOADJAMVPFPMDZZPWICIIIEEONTMDZVJWZZTCMIPVTHURRHUFXDDKIPBSJJDIRBXHURUUAGAGBLRRYABRRZZYNTMSPBYXETXINRLILYLSIYRLJFMSZTMCCSUVHHEFIPOHPCPMGSSMJKGKKJNLAGPZPVGTWZGJOHZGXAWWHQAPROWCSOYXNAXHCIZIJZIAQWQDGCNTBBUUUIBVQYOERXEQIOEPLOBCLSIVRXDXFBIHYWILLHOTIOLBKEHKQMTXIOFVBPVFGWCVEJAXTKRRHUKWVJFWQGMFVVIQXDZWIFEAVYBCDJKAHDMTFWTRAKDKIFROUSIWIBREFJATNRPZONQDFIBBPYMYQMPFSLRREETMBNGBEOYYUACEGAKOYBHRBXENZVVVTHADXTNVQYLLDUNHIHOWSIRUKGHDTDGWSOLZQMDZPRUOJMCZUENDMCFHWFUXTWGPZOEQFOLUJHDEHKGMOQMXBEGJSHJLHDPJPPLMFCCWRAHQXJEKBYVFRNJFCPYKTQDIENXNJNTCAECKRGNQTZLYVEBAEKXOXEHAWGMNILBXAQNWFAKLTVZUFAKGWMPTUQTWZBMAJZIHDRBDUKAJSIYPYAWTPSVLUWZKTCSPWZWFCFUFOQGPAIXHXAVAVDGKGBKAJGDMUEHWURUDZCMLAPAJSIFOJYIFHCFPKUPMCFIDYUDAQACFIFOHJQUXNQAVGDAGOXNKAWZIEMJWYIRHQAKGQHWPEVLGQGPKNQZUJYUKAKNQACS

sa11

我可以这么写吗,,return n*(1+n)/2

望远

你这题目的要求把最基本的程序执行控制方法都给废了。。坐等大神==

蜂之谷

static int calculate(int n) { return n==1?n:calculate(n-1)+n; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java