In a gymnastics or diving competition, each contestant\'s score is calculated by
ID: 3771225 • Letter: I
Question
In a gymnastics or diving competition, each contestant's score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Write a program that allows the user to enter eight judges' scores and outputs the points received by the contestant. Format your output with two decimal places. (A judge awards points between 1 and 10, with 1 being the lowest and 10 being the highest.) For example, if the scores are 9.2, 9.3, 9.0, 9.9, 9.5, 9.5, 9.6, and 9.8, the contestant receives a total of 56.90 points. Test your program with the following scores: 1. 9.2, 9.3, 9.0, 9.9, 9.5, 9.5, 9.6, 9.8
Explanation / Answer
import java.util.Scanner;
// for double formatting
import java.text.DecimalFormat;
public class gymnasticScore
{
//---------------------------------------------------------
// Calculate the gymnastics score
//---------------------------------------------------------
public static void main(String[] args)
{
double maxScore = 0.0, minScore = 0.0, sumScore = 0.0,scoreAvg = 0.0, totalScore = 0.0 ;
// used to store 8 scores from 8 judges
double[] num = new double[8];
Scanner readinput = new Scanner(System.in);
// prompt user for inputs
System.out.print("Enter 8 scores out of ten points separated by a space: ");
// store all the input in num[]
for(int i=0;i<num.length;i++)
{
num[i] = readinput.nextDouble();
// sum up all the score
sumScore = sumScore + num[i];
}
// set initial value minScore to the first array element
minScore = num[0];
// iterate, compare for max and min score and store them
for(int j = 0;j< num.length; j++)
{
if( minScore > num[j])
{
minScore = num[j];
}
if( maxScore < num[j])
{
maxScore = num[j];
}
}
// discard the lowest and highest scores
totalScore = sumScore - (maxScore + minScore);
// find the average score, the number of scores = 8.0 – 2.0 = 6.0
scoreAvg = totalScore / 6.0;
// print all the related information
System.out.println("=====================================");
System.out.println("Your Lowest score is " + minScore);
System.out.println("Your Maximum score is " + maxScore);
System.out.println("Your Total point is " + totalScore);
// formatting the double output to two decimal places
DecimalFormat fmt = new DecimalFormat("0.##");
System.out.println("Your average point is " + fmt.format(scoreAvg));
System.out.println("=====================================");
System.out.println("=========CONGRATULATION!=============");
}
}
Output of the program:
Enter 8 scores out of ten points separated by a space: 9.2 9.3 9.0 9.9 9.5 9.5 9.6 9.8
=====================================
Your Lowest score is 9.0
Your Maximum score is 9.9
Your Total point is 56.9
Your average point is 9.48
=====================================
=========CONGRATULATION!=============
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.