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.
<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];
for (int i = 0; i < n; i++) {
scores[i] = input.nextInt();
}
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];
}
}
System.out.println(max3);
}
}