0

I have a task that requires me to create a calculator and display the equation (1 + 2 = 3) onto the console and also write the equation to a text file using java code. I have created the calculator, however, struggling to code how it would write to a file and display to the console at the same time. Any suggestions?

import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

    double num1, num2;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter first number:");

    /* We are using data type double so that user
     * can enter integer as well as floating point
     * value
     */
    num1 = scanner.nextDouble();
    System.out.print("Enter second number:");
    num2 = scanner.nextDouble();

    System.out.print("Enter an operator (+, -, *, /): ");
    char operator = scanner.next().charAt(0);

    scanner.close();
    double output;

    switch(operator)
    {
        case '+':
            output = num1 + num2;
            break;

        case '-':
            output = num1 - num2;
            break;

        case '*':
            output = num1 * num2;
            break;

        case '/':
            output = num1 / num2;
            break;

        /* If user enters any other operator or char apart from
         * +, -, * and /, then display an error message to user
         * 
         */
        default:
            
        System.out.printf("You have entered wrong operator");
        return;


    }

    System.out.println(num1+" "+operator+" "+num2+"= "+output);
}

}

doom
  • 1
  • 1

0 Answers0