import java.util.Scanner;
public class MinAndMaxNumbers {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int max, max2, number, size;
max = max2 = 0;
boolean error = false;
System.out.println("How many numbers? (min 2)");
do {
size = in.nextInt();
if(size < 2) {
error = true;
System.out.println("2 numbers at least, try again");
}
else
error = false;
}
while(error == true);
for (int i = 1; i <= size; i++)
{
System.out.println("Introduce number " + i + ":");
number = in.nextInt();
if(i == 1) { //if it's the first number, max and max are the same
max = max2 = number;
}
else { //for the rest of the numbers we'll do a check
if(number > max2) {
max = max2;
max2 = number;
}
}
}
System.out.println("Highest numbers: " + max + " and " + max2);
}
}
Powinien wyznaczać najwyższą liczbę i najwyższą zaraz po niej z tych które podał użytkownik.
Gdy podaję przykładowo liczby: 1,5,4,3 jako najwyższą prawidłowo podaję 5, jednak jako następną w kolejności podaje 1.