viewing paste AverageArrays | Java

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
import java.util.Scanner;
 
public  class   AverageArrays{
 
    public  static  void    main(String[] args){
        
        final int MAX_REPEAT = 5;
        int i = 0;
        int[] listi = new int[5];
        double[] listd = new double[5];
        
        Scanner input = new Scanner(System.in);
        while(i < MAX_REPEAT){
        System.out.println("Please input a number: ");
        listi[i] = input.nextInt();
        i++;
        }
        System.out.println("The average of these numbers is "+average(listi));
        i = 0;
        while(i < MAX_REPEAT){
        System.out.println("Please input a number: ");
        listd[i] = input.nextDouble();
        i++;
        }
        System.out.println("The average of these numbers is "+average(listd));
        
    }
    public static double average(int[] array){
        double average;
        int sum = 0;
        int i;
        for(i = 0; i < array.length; i++){
            
            sum += array[i];
            
        }
        average = (int)sum/array.length;
        
        return average;
    }
    public static double average(double[] array){
        double average;
        double sum = 0;
        int i;
        for(i = 0; i < array.length; i++){
            
            sum += array[i];
            
        }
        average = sum/array.length;
        
        return average;
    }
}
Viewed 1507 times, submitted by Streusel.