Calculators are a fundamental tool in our everyday lives, whether it’s for math homework, budgeting, or even just making simple calculations. In this blog post, we will be creating a simple calculator using the switch…case statement in Java.
To start, we will need to create a new Java project and create a new class called “Calculator”. Inside this class, we will be creating a main method where our program will run.
First, we will need to declare our variables. We will be using two double variables, “num1” and “num2”, to hold the numbers that we will be performing calculations on. We will also need a char variable, “operator”, to hold the operator that the user inputs.
Next, we will create a switch statement that will take the operator as its input. Inside the switch statement, we will use the case statement to check the operator and perform the corresponding calculation.
Here is the code for our switch statement:
switch(operator) {
case '+':
System.out.println(num1 + num2);
break;
case '-':
System.out.println(num1 - num2);
break;
case '*':
System.out.println(num1 * num2);
break;
case '/':
System.out.println(num1 / num2);
break;
default:
System.out.println("Invalid operator");
}
In this code, we are using the “+”, “-“, “*”, and “/” operators to perform addition, subtraction, multiplication, and division, respectively. If the operator input is none of these, the program will output “Invalid operator”.
Finally, we will need to prompt the user for their input. We can use the Scanner class to read in the user’s input for the two numbers and the operator. Here is the code for that:
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter operator: ");
operator = input.next().charAt(0);
System.out.print("Enter second number: ");
num2 = input.nextDouble();
With this code, the program will prompt the user to enter their first number, operator, and second number. The program will then store these values in the corresponding variables.
Java Program
Putting it all together, here is the full code for our simple calculator:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
double num1, num2;
char operator;
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter operator: ");
operator = input.next().charAt(0);
System.out.print("Enter second number: ");
num2 = input.nextDouble();
switch(operator) {
case '+':
System.out.println(num1 + num2);
break;
case '-':
System.out.println(num1 - num2);
break;
case '*':
System.out.println(num1*num2);
break;
case '/':
System.out.println(num1 / num2);
break;
default:
System.out.println("Invalid operator");
}
}
}
Explanation
- The program imports the Scanner class from java.util package.
- The program defines a class called Calculator with a main method.
- Within the main method, the program declares two variables, num1, and num2, to hold the two numbers entered by the user. It also declares a variable called an operator to hold the arithmetic operator entered by the user.
- The program creates a Scanner object called input to read input from the user.
- The program prompts the user to enter the first number, which is stored in the num1 variable.
- The program prompts the user to enter the arithmetic operator, which is stored in the operator variable.
- The program prompts the user to enter the second number, which is stored in the num2 variable.
- The program uses a switch statement to evaluate the operator variable and perform the corresponding arithmetic operation using the num1 and num2 variables.
- If the operator entered by the user is “+”, the program performs num1 + num2 and prints the result.
- If the operator entered by the user is “-“, the program performs num1 – num2 and prints the result.
- If the operator entered by the user is “*”, the program performs num1 * num2 and prints the result.
- If the operator entered by the user is “/”, the program performs num1 / num2 and prints the result.
- If the operator entered by the user is not one of the accepted operators (+, -, *, /), the program prints “Invalid operator”.
Latest Posts:
- Java Program to Check Whether an Alphabet is Vowel or ConsonantJava is a versatile and widely used programming language known for its simplicity and readability. In this blog post, we’ll … Read more
- Java Program to Find ASCII Value of a characterIn this post, you will learn about finding and displaying ASCII value for any character. The American Standard Code for … Read more
- Data Types in JavaJava is a strongly typed programming language, which means that every variable and expression in Java has a data type. … Read more
- Armstrong Number in JavaIn this blog, we will be discussing how to check whether a given number is an Armstrong number or not … Read more
- Simple Calculator in Java using Switch caseCalculators are a fundamental tool in our everyday lives, whether it’s for math homework, budgeting, or even just making simple … Read more
- Switch Statement in JavaJava provides a powerful tool known as the “switch” statement, which allows developers to easily control the flow of their … Read more