Learn how to build a simple calculator in Java using switch statements with easy step-by-step instructions, source code, and clear explanations for beginners.
Creating a calculator is one of the best ways to start learning programming. In this article, we will create a simple calculator in Java using switch statements. This calculator will perform basic arithmetic operations like addition, subtraction, multiplication, and division based on user input.
A switch statement in Java is used to perform different actions based on different conditions. It works like an if-else-if ladder but makes the code cleaner and more readable when you have multiple conditions to check.
Takes two numbers as input from the user.
Takes an operator as input (+
, -
, *
, /
).
Performs the chosen operation using a switch statement.
Displays the result to the user.
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Take first number input
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
// Step 2: Take second number input
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
// Step 3: Ask the user for the operation
System.out.print("Choose an operation (+, -, *, /): ");
char operator = scanner.next().charAt(0);
double result;
// Step 4: Use switch statement to perform calculation
switch (operator) {
case '+':
result = num1 + num2;
System.out.println("Result: " + result);
break;
case '-':
result = num1 - num2;
System.out.println("Result: " + result);
break;
case '*':
result = num1 * num2;
System.out.println("Result: " + result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + result);
} else {
System.out.println("Error: Cannot divide by zero!");
}
break;
default:
System.out.println("Invalid operator! Please use +, -, *, or /.");
}
scanner.close();
}
}
import java.util.Scanner;
We import Scanner
class from the Java utility package so that we can take input from the user.
double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();
We ask the user to enter two numbers. These are stored as double
to allow decimal values.
char operator = scanner.next().charAt(0);
We ask the user to enter an operator like +
, -
, *
, or /
. We read the first character of the input using .charAt(0)
.
switch (operator) {
case '+':
...
case '-':
...
// other cases
}
The switch checks the value of operator
and runs the matching case block.
For each case, we perform the calculation and print the result. If the operator is invalid, the default
case handles it with a message.
Always check for division by zero to avoid errors.
switch
is case-sensitive, so only valid symbols will work.
Use scanner.close()
at the end to prevent resource leaks.
You’ve now learned how to create a basic calculator in Java using switch statements. This is a great beginner project to understand user input, control statements, and basic arithmetic operations.
Once you’re comfortable with this, try expanding the calculator by:
Adding more operations like modulus (%
)
Supporting multiple calculations in a loop
Handling invalid input more gracefully
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!