Ваш код был немного беспорядочным, я опубликую исправленную версию и объясню отдельные шаги.
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//firstable, you do not need to declare seperate variables for the number
//you want to enter, but you can use those (here currentNumber is the
//number typed in most recently, midNumber is the number before that,
//previousNumber is the one before that); they are initialized with 0 for
//reasons you will they later
int currentNumber = 0, midNumber = 0, previousNumber = 0;
boolean flag = false;
//a while loop, which asks for new numbers, as long as the user does not
//break it
while(true){
//the user enters a number, which is the new current number
System.out.print("Enter a number: ");
currentNumber = scanner.nextInt();
//if the entered number is 0 and there was one number entered before,
//the while loop breaks and the process ends
if(currentNumber == 0 && midNumber != 0){
break;
}
//there have to be at least three numbers in the sequence for it to be
//tested, otherwise the sequence is always true, so there have to be a
//midNumber and a previousNumber
if(previousNumber != 0 && midNumber != 0){
flag = false;
//the flag is only true, if the three entered numbers are either
//descending or ascending
if(
(previousNumber <= midNumber && midNumber <= currentNumber)
||
(previousNumber >= midNumber && midNumber >= currentNumber)
){
flag = true;
}
}
//the previous number is set to the midNumber, the midNumber is set to
//the currentNumber
previousNumber = midNumber;
midNumber = currentNumber;
//since the process has not been terminated by the user, the while-loop
//starts over
}
// for memory reasons, the scanner has to be closed, since it will not be
//used after the while-loop, it will be closed here
scanner.close();
//the flag is printed; true if the sequence is true, false if otherwise
System.out.println(flag);
}
}