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");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS