I have to create a five-function calculator. The input to my program will be a binary arithmetic expression, with the operator separated from the operands by a space like 24 * 4. The output will be the binary arithmetic expression, followed by an equal sign, surrounded by spaces, followed by the answer.
I can assume that the operands will be floating-point numbers (use doubles). The operator should be one of the following: *, /, +, -, or %. Here are important additional specifications:
-Emit an appropriate prompt (i.e. Please enter an equation with spaces between operands and operator.)
-If the operator is not one of *, /, +, -, or %, issue an appropriate error message (i.e. Sorry, '&' is not recognized).
-If operator is "/" and the second operand is zero, issue an appropriate error message.(i.e. Cannot divide by zero).
Please help! I tried over and over again and my program will either not run or it won't compile. I don't know what I'm doing wrong.
import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter a binary arithmetic expression, with spaces between operands and operator. \n"); double inputOne = in.nextDouble(); double inputTwo = in.nextDouble(); char operator = 0; if (operator == '+') System.out.printf("%f + %f = %f", inputOne, inputTwo, inputOne + inputTwo); if (operator == '-') System.out.printf("%f - %f = %f", inputOne, inputTwo, inputOne - inputTwo); if (operator == '*') System.out.printf("%f * %f = %f", inputOne, inputTwo, inputOne * inputTwo); if (operator == '/') { if (inputTwo == 0) { System.out.printf("Cannot divide by 0"); } else System.out.printf("%f / %f = %f", inputOne, inputTwo, inputOne / inputTwo); } if (operator == '%') { System.out.printf("%f % %f = %f", inputOne, inputTwo, inputOne % inputTwo); } } }