I have an array of integers and I want to go through it using a for loop and find the minimum value. I have a variable set for minimum which is compared with the current index as the program moves through the list, however, it prints every single index it comes across which is less than the variable minimum, instead of printing only one in the end of the program. Having the System.out.println
statement outside the for loop would solve the problem but it does not let me do that as it says there are undefined variables.
int Min = age[0];
for(int i = 0; i < age.length; i++) {
if (age[i] < Min) {
int minIndex = i;
System.out.println("Name of youngest athlete:" +
names[minIndex] + "\n age:" + age[minIndex] + "\n points:" +
points[minIndex]);
}
}