java program for a calculator using switch case

Solutions on MaxInterview for java program for a calculator using switch case by the best coders in the world

showing results for - "java program for a calculator using switch case"
Elyna
16 Mar 2019
1import java.util.Scanner;
2public class Main
3{
4public static void main(String[] args) 
5{
6Scanner reader = new Scanner(System.in);
7System.out.print("Enter two numbers");
8double first = reader.nextDouble();
9double second = reader.nextDouble();
10System.out.print("Enter an operators");
11char operator = reader.next().charAt(0);
12double result;
13switch(operator)
14{
15case '+':
16result = first + second;
17break;
18case '-':
19result = first - second;
20break;
21case '*':
22result = first * second;
23break;
24case '/':
25result = first / second;
26break;
27default:
28System.out.printf("operator is correct");
29return;
30}
31System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
32}
33}