
Write a program that will evaluate simple expressions such as 17 + 3 and 3.14159 * 4.7. The expressions are to be typed in by the user. The input always consist of a number, followed by an operator, followed by another number. The operators that are allowed are +, -, *, and /. Your program should read an expression, print its value, read another expression, print its value, and so on. The program should end when the user enters 0 as the first number on the line.

import java.util.*;
public class Calculate {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
double num1;
double num2;
char operator;
double result;
System.out.println("Enter expressions such as 17 + 3 or 3.14159 * 4.7");
System.out.println("Use any of the operators +, -, *, /.");
System.out.println("To end, enter a 0.");
while (true){
System.out.println();
System.out.println("Enter Expression: ");
num1 = input.nextDouble();
if (num1 == 0)
break;
operator = input.next().charAt(0);
num2 = input.nextDouble();
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
System.out.println("Unknown operator: " + operator);
continue;
}
System.out.println("Result is " + result);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.