Ternary Operator
Completed
Read an integer and print N is positive, N is negative or N is zero using nested ternary operators in one statement.
Input
One integer.
Output
One line.
Example
Input
5
Output
5 is positive
Editor
Java
Hints
condition ? valueIfTrue : valueIfFalse
Solution explanation
The ternary operator is an expression, so it can be used inside assignments.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String kind = n > 0 ? "positive" : n < 0 ? "negative" : "zero";
System.out.println(n + " is " + kind);
}
}