简体   繁体   中英

How to pull the maximum and minimum values from an array?

I am using arrays for a programming project due tonight. I am able to add up all the numbers of the array, but from there I am unable to remove the maximum and minimum values from it. My attempt and the actual project description is below...

In the sport of diving, seven judges award a score between 0 and 10, where each score may be a floating-point value. The highest and lowest scores are thrown out and the remaining scores are added together. The sum is then multiplied by the degree of difficulty for that dive. The degree of difficulty ranges from 1.2 to 3.8 points. The total is then multiplied by 0.6 to determine the diver's score.

Write a computer program that inputs a degree of difficulty and seven judges' scores and outputs the overall score for that dive. The program should ensure that all inputs are within the allowable data ranges.

package baker;

import java.util.Scanner;

public class DiveScoreDriver {
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        double total = 0;
        double totalFinal = 0;
        double divingScores[] = new double[7];
        double input;
        double difficultyInput = 0;
        double minimum = divingScores[0];
        double maximum = divingScores[0];

        for (int i = 1; i < divingScores.length + 1; i++) 
        {
            System.out.println("Judge " + i + " please enter your score.");
            input = keyboard.nextDouble();
            System.out.println();

            if(input < 0 || input > 10) 
            {
                System.out.println("Invalid Score");
                    return;
            }
            else 
            {
                total += input; 
            } 
        }

        while (difficultyInput < 1.2 || difficultyInput > 3.8) 
        {
            System.out.println("Difficulty Rating: ");
            difficultyInput = keyboard.nextDouble();
        }


        for(int i = 0; i < divingScores.length; i++)
    {
            if(divingScores[i] < minimum)
        minimum = divingScores[i];
            if(divingScores[i] > maximum)
        maximum = divingScores[i];
        }

        total = total - maximum - minimum;
        total = total * difficultyInput;
    total = total * 0.6;

        System.out.printf("\nThe overall score for the dive: %.1f\n", total);

    }
}

The portion in particular that I am struggling with is here:

  for(int i = 0; i < divingScores.length; i++)
    {
            if(divingScores[i] < minimum)
        minimum = divingScores[i];
            if(divingScores[i] > maximum)
        maximum = divingScores[i];
        }

        total = total - maximum - minimum;
        total = total * difficultyInput;
        total = total * 0.6;

The code runs and produces a correct output, but it does not seem to subtract the max and min values and the problem requests... Thanks for the help!

You have forgotten to add each judge's score to the array divingScores. You can fix this by changing the first for loop to the following:

for (int i = 0; i < divingScores.length; i++) 
    {
        System.out.println("Judge " + (i + 1) + " please enter your score.");
        input = keyboard.nextDouble();
        System.out.println();

        if(input < 0 || input > 10) 
        {
            System.out.println("Invalid Score");
                return;
        }
        else 
        {
            total += input;
            divingScores[i] = input;
        } 
    }

You should also initialize minimum as:

minimum = 0

If you do not, every score above 0 will not be considered for the minimum.

You never set the array values in the else branch within your for loop, it should look like this:

if(input < 0 || input > 10) {
    System.out.println("Invalid Score");
    return;
} else {
    divingScores[i] = input;
    total += input; 
} 

Before the second loop, you can use Java 8 functional programming to get the minimum and maximum like this, which is shorter:

double minimum = Arrays.stream(divingScores).min().getAsDouble();
double maximum = Arrays.stream(divingScores).max().getAsDouble();

Alternatively , you should initialize the minimum and maximum values properly, one way to do this in general for at least one element in the array is:

double minimum = Double.MAX_VALUE; // Use your maximum in your case
double maximum = Double.MIN_VALUE; // Use your minimum in your case

You can sort the array and then add the array elements except first and last element of sorted array which will automatically remove the minimum and maximum

Arrays.sort(divingScores);
double ans=0;
for(int i=1;i<divingScores.length-1;i++){
    System.out.println(divingScores[i]);
    ans+=divingScores[i];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM