fork download
  1. import java.util.Arrays; // Importing used class only
  2.  
  3. public class Main {
  4. /**
  5.   * Calculates the average of given integer array.
  6.   * @param Numbers Array of integers to calculate
  7.   * @return {int} Rounded average of all numbers on the array
  8.   */
  9. public static int IntAverage(int[] Numbers) {
  10. double Result = 0; // Initializing with a value
  11. // Foreach variation of the loop
  12. for (int Number : Numbers) {
  13. Result += Number;
  14. }
  15. // Typecast already rounds number
  16. return (int) (Result / Numbers.length);
  17. }
  18.  
  19. public static void main(String[] args) {
  20. int[] TestValues = {2, 3, 5, 7, 11, 13, 17, 19, 23}; // Pre-defined test values
  21. // Printing output to console
  22. System.out.println("Numbers: " + Arrays.toString(TestValues));
  23. System.out.println("Average: " + IntAverage(TestValues));
  24. System.out.println(); // Empty line after execution
  25. }
  26. }
Success #stdin #stdout 0.1s 36056KB
stdin
Standard input is empty
stdout
Numbers: [2, 3, 5, 7, 11, 13, 17, 19, 23]
Average: 11