Java Task 2 Test Scores Write a program that asks the user
Java
Task #2 – Test Scores Write a program that asks the user to enter 5 test scores and calculate their average. As your input, assume valid test scores can range from 0 to 100 [Note that you do NOT need to verity the validity of the scores]. After 5th entry is made, the program displays each test score that was entered (on the same line as separated by a comma) and the average of the scores on a new line. I expect you to provide me with the source code for this task.
Solution
TestScore.java
import java.util.Scanner;
public class TestScore {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int scores[] = new int[5];
for(int i=0;i<scores.length; i++){
System.out.print(\"Enter a score \"+(i+1)+\": \");
scores[i]=scan.nextInt();
}
int sum = 0;
System.out.println(\"Given test scores are: \");
for(int i=0;i<scores.length; i++){
if(i == scores.length-1){
System.out.println(scores[i]);
}
else{
System.out.print(scores[i]+\", \");
}
sum = sum + scores[i];
}
double average = sum/(double)5;
System.out.println(\"Average is \"+average);
}
}
Output:
Enter a score 1: 40
Enter a score 2: 50
Enter a score 3: 6
Enter a score 4: 70
Enter a score 5: 80
Given test scores are:
40, 50, 6, 70, 80
Average is 49.2
