Compute three class statistics from a list of scores: the highest, the lowest, and the average.
You start with:
List<Integer> scores = Arrays.asList(92, 78, 85, 67, 74, 88, 91, 58);
Build:
Highest: 92 Lowest: 58 Average: 79
Rules:
highestis the largest value inscores.lowestis the smallest value inscores.averageis the sum of all scores divided by how many there are, rounded down (integer division).- Print
"Highest: ","Lowest: "and"Average: "on three lines, in the order of the sample output (System.out.println("Highest: " + highest);).
Use
Collections.max / Collections.min on a List<Integer>; sum with a loop.
List<Integer> scores = Arrays.asList(92, 78, 85, 67, 74, 88, 91, 58);
int highest = Collections.max(scores);
int lowest = Collections.min(scores);
int total = 0;
for (int s : scores) total += s;
int average = total / scores.size();
System.out.println("Highest: " + highest);
System.out.println("Lowest: " + lowest);
System.out.println("Average: " + average);
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
