Java Online Compiler
Example: Check Number Sign with Ternary Operator in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Check Number Sign with Ternary Operator import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Create a Scanner object Scanner scanner = new Scanner(System.in); // Step 2: Prompt and read the number System.out.print("Enter a number: "); int number = scanner.nextInt(); // Using int for this example // Step 3: Use the ternary operator to determine the sign and print String result = (number > 0) ? "positive" : (number < 0) ? "negative" : "zero"; System.out.println(number + " is " + result + "."); // Step 4: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS