Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this program i am finding the score of the bronze medallist(s). i am writing program that reads from standard input a series of space-separated non-negative integers no greater than 10,000. My program is to write, to standard output, the integer value corresponding to the score of the bronze medallist. Note, that multiple contestants can tie with the same score, however, there will only be one gold, silver, and bronze medallist. In the case of ties, the competitor with lowest accumulated time to take their turn wins. Your program need only report the score of the bronze medallist.

• The program should read from standard integers, each integer being between zero (0) and ten thousand (10,000), and then writes to standard out a single integer representing the score of the bronze medalist

• When trying to run the program it should run like this :

javac Program.java

echo 27 3 9 3 6 27 10 15 8 | java Program

15

What I have tried:

This is the code i have at the moment, however it always returns -1, instead of the third place.

Java
<pre>import java.util.Scanner;

public class Program {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int n = args.length;
        int[] scores = new int[n];

        // read in scores
        for (int i = 0; i < n; i++) {
            scores[i] = input.nextInt();
        }

        // find the third highest score
        int max1 = -1, max2 = -1, max3 = -1;
        for (int i = 0; i < n; i++) {
            if (scores[i] > max1) {
                max3 = max2;
                max2 = max1;
                max1 = scores[i];
            } else if (scores[i] > max2 && scores[i] < max1) {
                max3 = max2;
                max2 = scores[i];
            } else if (scores[i] > max3 && scores[i] < max2) {
                max3 = scores[i];
            }
        }

        // output the third highest score
        System.out.println(max3);
    }
}
Posted
Updated 1-Mar-23 9:35am

1 solution

Try this:
Java
import java.util.Scanner;

public class Program {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int n = 0;
        int score;

        // read in scores
        int max1 = -1, max2 = -1, max3 = -1;
        while (input.hasNext()) {
            score = input.nextInt();

            if (score > max1) {
                max1 = score;
            } else if (score > max2) {
                max2 = score;
            } else if (score > max3) {
                max3 = score;
            }
        }

        // output all scores
        System.out.println(max1);
        System.out.println(max2);
        System.out.println(max3);
    }
}
 
Share this answer
 
Comments
CPallini 1-Mar-23 15:55pm    
5.
Richard MacCutchan 1-Mar-23 16:16pm    
Thanks; you can go to bed now :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900