Write a program to ask for the scores of a set of students o
Write a program to ask for the scores of a set of students (one at a time ) and to compute and display the class average as follows: How many students? 5 What is the score for students #1? 78 What is the score for students #2? 90 What is the score for students #3? 88 What is the score for students #4? 68 What is the score for students #5? 98 The class average is average ?? <= compute and display the class average
Solution
StudentScores.java
import java.util.Scanner;
public class StudentScores {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print(\"How many students? \");
int n = scan.nextInt();
int scores[] = new int[n];
for(int i=0; i<scores.length; i++){
System.out.print(\"What is the score for students #\"+(i+1)+\"? \");
scores[i] = scan.nextInt();
}
int sum = 0;
double average = 0;
for(int i=0; i<scores.length; i++){
sum = sum + scores[i];
}
average = sum/(double)scores.length;
System.out.println(\"The class average is average \"+average);
}
}
Output:
How many students? 5
What is the score for students #1? 78
What is the score for students #2? 90
What is the score for students #3? 88
What is the score for students #4? 68
What is the score for students #5? 98
The class average is average 84.4
