java - Sum of Array using for loop returning 0 -
i have written code below should return sum of array. keeps returning 0. please can this?
public class sumarray { public static void main (string [] args) { system.out.println(sumarrayof()); } public static int sumarrayof(){ int k; int sum = 0; int[] bs = new int[20]; (k=0; k<bs.length; k++) { sum = sum + bs[k]; } return sum; } }
your method should receive array parameter, instead of generating array itself.
like this:
public static int sumarrayof(int[] data) { int k; int sum = 0; (k=0; k<data.length; k++) { sum = sum + data[k]; } return sum; }
Comments
Post a Comment