Hello Java Gurus:
I am trying to find 3 largest numbers in an array using a single For loop but my following code is still showing randomly sorted numbers. Any help/suggestion will be much appreciated.
public class largest3 {
static int m, n, o;
static int array[] = new int[100];
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
array[i] = (int) (Math.random() * 100);
}
m = array[0];
n = array[1];
o = array[2];
for (int i = 0; i < 10; i++) {
if (array[i] > m){
o = n;
n = m;
m = array[i];
}
else if(array[i] > n) {
o = n;
n = array[i];
}
else if(array[i] > o) {
o = array[i];
}
}
for (int i = 0; i < 10; i++) {
System.out.println(array[i]);
}
}
}