we have to create a calculator and take input from the user that which operation perform like addition subtraction multiplication by choosing an operation should be perform using switch statement in java code example

Example: how to make a calculator using switch case in java

/* Create Switch cases */
Scanner sc= new Scanner(System.in);
int firstNum= sc.nextInt();
int secondNum= sc.nextInt();
System.out.println("Enter an operator (+, -, *, /):");
char operator= sc.next().chartAt(0);
double result=0;
switch(operator){
  case "+":
    result+= firstNum+ secondNum;
    break;
  case "-":
    result+= firstNum-secondNum;
    break;
  case "*":
    result+= firstNum*secondNum;
    break;
  case "/":
    result+= firstNum/secondNum;
    break;
}

Tags:

Java Example