find the average age

There are several students in the class, give the age of each student (integer), find the average age of all students in the class, and keep it to two decimal places.
Input
The first line has an integer n (1 <= n <= 100) representing the number of students. The next n lines each contain an integer representing the age of each student, ranging from 15 to 25.
Output
Output a line containing a floating point number, the desired average age, rounded to two decimal places.
Sample input
2
18
17

Sample output
17.50

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int n;
        double a,sum=0;
        double d=0;
        Scanner scanner=new Scanner(System.in);
        n=scanner.nextInt();
        for(int i=0;i<n;i++)
        {
            a=scanner.nextDouble();
            sum=sum+a;
        }
        d=sum/n;
        System.out.println(String.format("%.2f", d));

    }

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326615835&siteId=291194637