Created
August 19, 2020 12:04
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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