Skip to content

Instantly share code, notes, and snippets.

@staffordsmith83
Created August 19, 2020 12:04
Show Gist options
  • Save staffordsmith83/d731c3a96396f145c3e3066c7ebeba90 to your computer and use it in GitHub Desktop.
Save staffordsmith83/d731c3a96396f145c3e3066c7ebeba90 to your computer and use it in GitHub Desktop.
Reads a sequence of integer numbers and outputs true if the sequence is ordered (in ascending or descending order), otherwise, false.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int myIntA;
int myIntB = sc.nextInt();
boolean ascendingResult = true;
boolean descendingResult = true;
for (;;) {
myIntA = myIntB;
myIntB = sc.nextInt();
if (myIntB == 0) {
break;
}
// check for ascending sequence
if (ascendingResult && myIntB < myIntA) {
ascendingResult = false;
continue;
}
// check for descending sequence
if (descendingResult && myIntB > myIntA) {
descendingResult = false;
continue;
}
if (!descendingResult && !ascendingResult) {
break;
}
}
boolean result = ascendingResult || descendingResult;
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment