java Write a program that reads an unspecified number of scores and determines h
ID: 3760177 • Letter: J
Question
java
Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average. Enter a negative number to signify the end of the input. Assume that the maximum number of scores is 100, and that the user will enter only numbers. Sample run: Enter a new score (-1 to end): Enter a new score (-1 to end): Enter a new score (-1 to end): Enter a new score (-1 to end): Enter a new score (-1 to end): Enter a new score (-1 to end): average: 3.60 Total number of scores: 5 Number of scores above or equal to the average: 3 Number of scores below the average: 2Explanation / Answer
public class Scores {
/**
* @param args
*/
static double average;
static double totalScore=0;
static int belowAvg=0,aboveAvg=0;
static ArrayList<Integer> scores=new ArrayList<Integer>();
public static void main(String[] args) {
int input;
Scanner reader = new Scanner(System.in);
do
{
System.out.print("Enter a new score (-1 to end): ");
input = reader.nextInt();
scores.add(input);
}while(scores.size()<100 && input!=-1);
reader.close();
for(int i=0;i<scores.size();i++)
totalScore+=scores.get(i);
average=totalScore/scores.size();
for(int i=0;i<scores.size();i++)
{
if(scores.get(i)>=average)
aboveAvg++;
else
belowAvg++;
}
System.out.println("Average:"+average);
System.out.println("Total number of scores:"+scores.size());
System.out.println("Number of scores above or equal to the average:"+aboveAvg);
System.out.println("Number of scores below the average:"+belowAvg);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.