Write a static method called averageScores that takes as a parameter a Scanner containing a series of student records and that prints a summary of each student record. A student record will begin with a name followed by a sequence of integer homework scores. The name is guaranteed to be one word composed of letters. You may assume that each student has at least one homework score. Your method should produce one lines of output for each student showing the student's name and average score. For example, if a Scanner called records contains the following:
John 71 83 94 81 Sally 94 85 Fred 90 95 82 85
and the following call is made:
averageScores(records);
the following output should be produced:
John average = 82.25 Sally average = 89.5 Fred average = 88.0
You are to exactly reproduce the format of this output. Notice that line breaks in the input are not meaningful and that the average is not rounded. You may not construct any extra data structures to solve this problem.
import java.util.Scanner; public class AverageScores { public static void averageScores(Scanner in) { String name; double total, count; while (in.hasNext()) { name = in.next(); total = 0; count = 0; while (in.hasNextInt()) { total += in.nextInt(); ++count; } System.out.println(name + " average = " + (total/count)); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); averageScores(in); in.close(); } }
Get Answers For Free
Most questions answered within 1 hours.