java program where the user chooses his operation without case code example

Example: java calculator

package main;

import java.util.Scanner;

public class calculator {

    public static void greetings(){
        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
    }

    public static void instruction(){
        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
    }

    public static void calculator(){
        Scanner keyboardInput = new Scanner(System.in);
        String calculator;

        do {
            System.out.print("Enter your first digit: ");
            double num1 = keyboardInput.nextDouble();

            System.out.print("Enter your operator: ");
            String op = keyboardInput.next();

            System.out.print("Enter your second digit: ");
            double num2 = keyboardInput.nextDouble();

            System.out.print("= ");

            if (op.equals("+")){
                System.out.print(num1 + num2);
                System.out.println("\nYou were adding");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("-")){
                System.out.print(num1 - num2);
                System.out.println("\nYou were subtracting");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("*")){
                System.out.print(num1 * num2);
                System.out.println("\nYou were multiplying");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("/")){
                System.out.print(num1 / num2);
                System.out.println("\nYou were dividing");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("%")){
                System.out.print(num1 % num2);
                System.out.println("\nYou were remanding");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else{
                System.out.println("Invalid operator");
            }
            System.out.print("Would you like to restart the calculator? (y or n): ");
            calculator = keyboardInput.next();
        }
        while (calculator.equals("y"));

        if (calculator.equals("n")){
            System.out.println("Leave then kiddo ;-;");
        }
    }


    public static void main(String[] args) {
        greetings();
        instruction();
        calculator();
    }
}

Tags:

Java Example