0

Ordered or Unordered sequence

I am facing a problem in solving the question below:- (can someone please help????) Write a program that reads a sequence of integer numbers and outputs true if the sequence is ordered (in ascending or descending order), otherwise, false. Keep in mind, if a number has the same value as the following number, it does not break the order. The sequence ends with 0. Do not consider this number as a part of the sequence. The sequence always has at least one number (excluding 0). code which I wrote(For this code I am getting runtime error)..... ```import java.util.Scanner; class Main { public static void main(String[] args) { // put your code here Scanner scanner = new Scanner(System.in); int number1 = scanner.nextInt(); int number2 = scanner.nextInt(); int number3 = scanner.nextInt(); int mid = number2; int next = number3; int prev = number1; int flag = 1; boolean val = true; if (number1 > 0) { while (val) { if (prev <= mid && mid <= next || prev >= mid && mid >= next) { flag = 1; int number4 = scanner.nextInt(); prev = number2; mid = number3; next = number4; } else { flag = 0; } } } if (flag == 1) { System.out.println(true); } else { System.out.println(false); } } }```

26th May 2020, 6:40 PM
Pranav Nigam
Pranav Nigam - avatar
1 Antwort
+ 1
while (val) is always true
26th May 2020, 6:53 PM
Abhay
Abhay - avatar