sum2

From ggc

Jump to: navigation, search

http://www.javabat.com/prob?id=Array1.sum2


public int sum2(int[] nums) {

  int sum = 0;
 for (int i=0; i<nums.length; i++) {
   sum = sum + nums[i];
 }
 return sum;

}


I can get all but 2 to pass I need help with the code to make it all work.


Solution

public int sum2(int[] nums) {

 if(nums.length >=2)
 return nums[0] + nums[1];
 if(nums.length == 1)
 return nums[0];
 else
 return 0;

}





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter