Calculator

12th August 2018

This is the Java code for a basic text-based calculator. The program asks for a number. When you enter the number, the program asks you to enter an operation. When you enter the operation, the the program asks you to enter another number. When you enter that other number, the console does the calculation and prints out the answer.

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        double first;
        double second;
        double answer;
        String operation;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        first = sc.nextDouble();

        System.out.print ("Enter operation: ");
        operation = sc.next();

        System.out.print("Enter another number: ");
        second = sc.nextDouble();

        if(operation.equalsIgnoreCase("+")) {
            answer = first + second;
            System.out.println(answer);
        }

        if(operation.equalsIgnoreCase("-")) {
            answer = first - second;
            System.out.println(answer);
        }

        if(operation.equalsIgnoreCase("*")) {
            answer = first * second;
            System.out.println(answer);
        }

        if(operation.equalsIgnoreCase("/")) {
            answer = first / second;
            System.out.println(answer);
        }
    }
}