HIDE NAV

Command Line Menu

The below command line program demonstrates how to create a basic menu that uses numeric inputs for choices.

Main.java


package ceccs;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        /**
         * We will have 2 set variables and user chooses
         * what to do with them ( + - * / )
         */
        double val1 = -6;
        double val2 = 1.5;
        double outVal = 0;
        /** choice:
         *      1 = add,  2 = sub   3 = mult    4 = div
         * */
        Scanner scan = new Scanner(System.in);
        int choice = scan.nextInt();

        if (choice == 1) {
            outVal = val1 + val2;
        } else if (choice == 2){
            outVal = val1 - val2;
        } else if (choice == 3){
            outVal = val1 * val2;
        }  else if (choice == 4){
            outVal = val1 / val2;
        }

        System.out.println(outVal);
    }
}