Challenge: Score Report
Completed
Read n scores. Using streams, print how many scores are 50 or more, the highest score, and the average with 1 decimal.
Input
First n (n ≥ 1), then n whole numbers.
Output
Passed: <count> Highest: <max> Average: <avg, 1 decimal>
Example
Input
5 78 92 45 60 30
Output
Passed: 3 Highest: 92 Average: 61.0
Editor
Java
Hints
count() gives a long.
mapToInt(x -> x) gives an IntStream with max() and average().
average() returns an OptionalDouble – call getAsDouble().
Solution explanation
Each line is a small stream: filter-and-count, then max, then average.
Show solution
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> s = new ArrayList<>();
for (int i = 0; i < n; i++) s.add(sc.nextInt());
System.out.println("Passed: " + s.stream().filter(x -> x >= 50).count());
System.out.println("Highest: " + s.stream().mapToInt(x -> x).max().getAsInt());
System.out.printf("Average: %.1f%n", s.stream().mapToInt(x -> x).average().getAsDouble());
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module