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.
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");
}
}
Comments
Leave a comment