Question #271739

Using the ternary operator ( ? : ), write a Java program that prompts the user to input an integer and then checks if the number is positive or negative or equal to zero.



Expert's answer

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = in.nextInt();
        System.out.println(number > 0 ? "Positive" : number < 0 ? "Negative" : "Zero");
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS