In: Computer Science
[JAVA] How to do this problem. we have to tell if given list of three number is in ascending, descending or no order.
Instructions
You will be given three integers: ?, ? and ? (read in for you). Then you will be given two boolean values that tell you which way the numbers are going - if the numbers are in increasing order or in decreasing order, respectively.
Details
Input
The program reads in:
a sequence of three integers: ?, ? and ?
boolean isIncreasing: set to True if the sequence of integers is increasing, i.e. ?<?<? (note, we are reading in either 0 for False or 1 for True)
boolean isDecreasing: set to true if the sequence of integers is decreasing, i.e. ?>?>?
Processing
Output
Given code:
import java.util.Scanner;
public class PoD {
public static void main (String [] args) {
Scanner in = new Scanner
(System.in);
//Input
int i = in.nextInt();
int j = in.nextInt();
int k = in.nextInt();
boolean isIncreasing = in.nextInt()
== 1 ? true : false;
boolean isDecreasing = in.nextInt()
== 1 ? true : false;
System.out.println("Original order:
" + i + " " + j + " " + k);
if ( boolean isIncreasing = 1) {
System.out.println("The numbers are strictly
increasing.");
}
else if ( boolean isDecreasing = 1) {
System.out.println("The numbers are strictly
decreasing.");
}
else {
System.out.println("There is no strict order.");
}
}
}
import java.util.Scanner;
public class PoD {
public static void main (String [] args) {
Scanner in = new Scanner (System.in);
//Input
int i = in.nextInt();
int j = in.nextInt();
int k = in.nextInt();
boolean isIncreasing = in.nextInt() == 1 ? true : false;
boolean isDecreasing = in.nextInt() == 1 ? true : false;
System.out.println("Original order: " + i + " " + j + " " + k);
if (isIncreasing) {
System.out.println("The numbers are strictly increasing.");
}
else if (isDecreasing) {
System.out.println("The numbers are strictly decreasing.");
}
else {
System.out.println("There is no strict order.");
}
}
}
You just made a small mistake in "if" and "else if" conditions .
You already declared "isIncreasing" and "isDecreasing" variables so you don't need to say "boolean" again and you can't give "isIncreasing = 1" in conditions
I will submit my output screenshot below
Hope this will be helpful..