I am facing 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).....
class Main {
public static void main(String[] args) {
// put your code here
Scanner scanner = new Scanner(System.in);
int number1 = scanner.nextInt();
int number2;
int number3;
int number4;
int prev;
int mid;
int next;
int flag = 1;
boolean val = true;
while (val) {
if (number1 > 0) {
number2 = scanner.nextInt();
if (number2 > 0) {
number3 = scanner.nextInt();
if(number3 > 0){
prev = number1;
mid = number2;
next = number3;
if (prev >= mid && mid >= next || prev <= mid && mid <= next) {
number4 = scanner.nextInt();
prev = number2;
mid = number3;
next = number4;
flag = 1;
} else {
break;
}
} else {
if (number1 >= number2 && number2 >= number3 || number1 <= number2 && number2 <= number3) {
flag = 1;
} else {
flag = 0;
}
}
} else {
if (number1 >= number2 || number1 <= number2) {
flag = 1;
} else {
flag = 0;
}
}
} else {
flag = 0;
}
}
if (flag == 1) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}```
Here is one another solution:--(finally optimized it).
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int oldNum = scanner.nextInt();
int newNum;
int asc = 0;
int dec = 0;
while ((newNum = scanner.nextInt()) != 0) {
if (newNum > oldNum) {
asc++;
}
if (newNum < oldNum) {
dec++;
}
oldNum = newNum;
}
System.out.println(asc == 0 || dec == 0);
}
}```
Your code was a little bit messy, I will post a cleaned up version and explain the single steps.
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);
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int countOfNumbers = -2, ascPairs = 0, descPairs = 0;
boolean isOrder = false;
int temp = scanner.nextInt();
countOfNumbers++;
int first = temp;
while (true) {
temp = scanner.nextInt();
countOfNumbers++;
if (temp == first) {
ascPairs++;
descPairs++;
}
else if (temp > first && temp != 0) {
first = temp;
ascPairs++;
isOrder = true;
}
else if (temp != 0){
first = temp;
descPairs++;
isOrder = true;
}
else {
break;
}
}
scanner.close();
System.out.println((countOfNumbers == ascPairs && isOrder)
|| (countOfNumbers == descPairs && isOrder)
|| (countOfNumbers == 0));
// last string is needed if validator considers
// one number like an ordered sequence
}
}
Just made small changes from the already given solution.
This Solution is Optimal for the following input case:
100 101 102 103 104 105 106 105 104 0
Ans: False. (As it is Neither in Ascending or descending order).
class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int current = 0, mid = 0, previous = 0;
/* Initialized three variables. current denotes current entered element.
mid denoted element before current element. previous denotes element before mid element.*/
boolean flag = true;
/* "input case : 1000 0 " , the given input case
must return true as it is in order.*/
while(true){ //while loop to take user input until and unless a specified condition is satisfied.
current = sc.nextInt(); // to get input from user and set as current value.
if(current == 0 && mid != 0){
break;
}
/* if there is a certain condition where current element is zero and
previous element is having non-zero element. then we break the loop.*/
if(previous != 0 && mid != 0 ) {
if( (previous <= mid && mid <= current) || (previous >= mid && mid >= current) ) {
flag = true;
} else {
flag = false;
break;
}
}
/* ->in above two if conditions are used in first IF we check whether previous and
mid element are having non-zero element. in second IF condition we check that whether these three consecutive
element are in ascending or descending order.
->if any one of the condition is true we set flag as "true".
->In else section we set flag as "false" and break the loop when the three consecutive sequence are not in
ascending or descending order. */
previous = mid;
mid = current;
/* finally , we assign the mid element to previous and current element to mid , so to keep
updating the sequence of consecutive number.*/
} //while loop is closed.
sc.close(); //closing the Scanner.
System.out.println(flag); //State of the flag is printed.
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.