HIDE NAV

Positive-Negative-Neither

The below command line program demonstrates how to branch code using an if else conditional.
It classifies user input as positive, negative or neither.

Main.java


package ceccs;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int input;

        System.out.println("Enter an integer");

        Scanner scan = new Scanner(System.in);
        input = scan.nextInt();

        if ( input == 0) {
            System.out.println("The number is neither positive nor negative");
        } else {
            if (input > 0) {
                System.out.println("The number is positive");
            } else {
                System.out.println("The number is negative");
            }
        }
    }
}