Menu

Written Method Called Sum Int Parameter Nums Want Sum Method Compute Sum Nums Code Bug Per Q43887457

We have written a method called sum with an int[] parameternums. We want our sum method to compute the sum of nums, but ourcode has a bug (or perhaps bugs?) in it. TASK: Fix the bug(s).

static int sum(int[] nums) {
int total = 0;
for(int num : nums) { // for-each loop
total += num;
}
}

Sample Input:

1 2 3 4 5

Sample Output:

15

Expert Answer


Answer to We have written a method called sum with an int[] parameter nums. We want our sum method to compute the sum of nums, but…

OR