-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindAverageAge.java
46 lines (36 loc) · 1.26 KB
/
FindAverageAge.java
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
import java.util.Scanner;
public class FindAverageAge {
public static double calculateAverage(int[] age) {
double total = 0.0;
for (int i : age) {
total += i;
}
return total / (double) age.length;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter no.of employees:");
int n = scanner.nextInt();
if (n < 2) {
System.out.println("Please enter a valid employee count");
} else {
System.out.println(String.format("Enter the age for %d employees:", n));
int[] age = new int[n];
boolean Valid = true;
for (int i = 0; i < n; ++i) {
int a = scanner.nextInt();
if (a < 28 || a > 40) {
Valid = false;
break;
}
age[i] = a;
}
if (Valid == true) {
double average = calculateAverage(age);
System.out.println(String.format("The average age is %.2f", average));
} else {
System.out.println("Invalid");
}
}
}
}