java - Making a method for an array -


i have array takes student grades input. have make method find average of numbers within array. here code have far...

int mark = integer.parseint(input.gettext());     if(mark <= 100){     marks.add(integer.parseint(input.gettext()));       input.settext("");     warninglbl.settext("");     }     else{         warninglbl.settext("please enter number between 0-100");     } 

i want take contents in array 'marks' , average them append in text area

   public static double getaverage(int[] marks) {     int sum = 0;     for(int : marks) sum += i;     return ((double) sum)/marks.length; } 

this method have find average, dont know how use method , print in text area

if want find average stored in array , try following function.

public static double average(int[] marks) { int sum = 0; double average; for(int element: marks) {     sum = sum + element; } average = (double)sum / marks.length; return average; } 

hope helps ! :)

the above method used find average in array(primitive type) , find average in list type array(wrapper class) , have iterate through each element in list way , required calculations :

public static string average(integer[] marks) { int sum = 0; double average; (int = 0; < marks.size(); i++)  {     sum = sum + marks.get(i); } average = (double) sum / marks.size(); string averagestring = double.tostring(average); return averagestring; } 

this returns average in string type directly.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -