In: Computer Science
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
public class Main
{
public static void main(String[] args) {
//initialize array of names
String[] names = {
"Alexa", "Lilly", "Sharon", "Emma", "Joey", "Matt",
"phoebe", "Jack", "David", "James"};
//initialize array of running time ffor each runner
int[] runningtime = {
402, 341, 388, 278, 329, 445, 341, 273, 334, 500};
//initialoze temp to 0
int temp = 0;
for (int i = 0; i < runningtime.length; i++) {
/* fastest runner is the one who completes distance
in minimum running time. find the minimum running time from the array
runningtime and store the index of minimum running time in temp variable.
if running time of ith player is lower compare to first player
then temp becomes i. */
if (runningtime[i] < runningtime[temp]) {
temp = i;
}
}
//print the fastest runner as temp represents the index of the fastest runner
System.err.println("Fastest runner: " + names [temp]);
System.err.println( "Running Time of "+ names[temp] + ": " + runningtime[temp]);
}
}
OUTPUT: