Java method to sum any number of ints

后端 未结 7 712
不思量自难忘°
不思量自难忘° 2021-01-02 08:25

I need to write a java method sumAll() which takes any number of integers and returns their sum.

sumAll(1,2,3) returns 6
sumAll() returns 0
sum         


        
相关标签:
7条回答
  • 2021-01-02 09:12

    You could do, assuming you have an array with value and array length: arrayVal[i], arrayLength:

    int sum = 0;
    for (int i = 0; i < arrayLength; i++) {
        sum += arrayVal[i];
    }
    
    System.out.println("the sum is" + sum);
    

    I hope this helps.

    0 讨论(0)
提交回复
热议问题