java - How do you calculate average of an array with the values inserted from user? -


i'm trying average of ages plugged in user. try ask how many students in class , ages are. after that, displays ages in ascending order, however, i'm supposed calculate average age. why guys think program isn't working? feedback helps guys, , thank in advance. (this rough outline of want do) sorry if code didn't insert correctly.

package averageage; import javax.swing.joptionpane; import java.util.arraylist; import java.util.list; import java.util.collections;  public class averageage {      public static void main(string[] args) {         int avg = 0;         int n = integer                 .parseint(joptionpane                         .showinputdialog("enter number of how many people in classroom:"));         n = n - 1;         // * n defined number of students-1 because list starts @         // 0.         list<integer> ages = new arraylist<>();         // *defined list.         (int = 0; <= n; i++) {             ages.add(integer.parseint(joptionpane                     .showinputdialog("enter ages, program stop @ length limit.")));         }         collections.sort(ages);         (int counter : ages) {             joptionpane.showmessagedialog(null,                     "the ages in ascending order:\n" + counter);         }         n++;     }      // *sets value of n original input.     public static int averageage(int[] ages) {         int sum = 0;         (int x = 0; x < ages.length; x++) {             sum = sum + ages[x];         }         int avg = sum / ages.length;         joptionpane.showmessagedialog(null, "the average age is: \n" + avg);         return avg;      } } 

you didn't call averageage method,i have changed code. hope work

package averageage;  import javax.swing.joptionpane; import java.util.arraylist; import java.util.list; import java.util.collections;  public class averageage {  public static void main(string[] args) {     int avg = 0;     int n = integer             .parseint(joptionpane                     .showinputdialog("enter number of how many people in classroom:"));     n = n - 1;     // * n defined number of students-1 because list starts @     // 0.     list<integer> ages = new arraylist<>();     // *defined list.     (int = 0; <= n; i++) {         ages.add(integer.parseint(joptionpane                 .showinputdialog("enter ages, program stop @ length limit.")));     }     /*collections.sort(ages);     (int counter : ages) {         joptionpane.showmessagedialog(null,                 "the ages in ascending order:\n" + counter);     }     n++;*/     averageage(ages); }  // *sets value of n original input. public static int averageage(list<integer> ages) {     int sum = 0;     (int x = 0; x < ages.size(); x++) {         sum = sum + ages.get(x);     }     int avg = sum / ages.size();     joptionpane.showmessagedialog(null, "the average age is: \n" + avg);     return avg;   } } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -