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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -