Switch case java калькулятор

Java Program to Implement Calculator Using Switch Case | Java Programs

Before directly jumping into the solutions, it is advisable to follow a few steps before only to avoid confusion at a later stage. First, we need to understand the problem statement thoroughly. Then, we have to look for any constraints given in the problem. After this, we have to decide on our inputs followed by determining our expected output for this problem. Only after this, we should arrive at a logical solution.

Here, our problem statement is to write a program for a calculator by making use of switch case. Our required inputs are two numbers (num1 and num2) and an operator of our choice using another variable (n). Our expected output is a variable (result) returning the answer based on the choice of operation made and input numbers.

To read inputs at runtime, we will make use of Scanner class in Java for any primitive datatype. For this, we will create an object for Scanner class which can be used to invoke methods of Scanner class. Prior that, we need to know the operations available for us to perform. If the choice is 1 then, it is addition, it 2 then, subtraction, if 3 then, multiplication, if 4 then, division and if 5 then, modulus.

Читайте также:  Php curl отключить проверку сертификата ssl

Scanner sc = new Scanner(System.in);

First, we have to read the choice stored in an integer variable (n). Then we check if the choice made is valid or not. If the value in the variable (n) is between 1 to 5 both included then it is a valid choice and we move to the logic else, we print the choice is invalid and come out of the calculation loop.

System.out.print(“entered Wrong Choice!\n”);

If the choice entered (n) is a valid choice then, we first read the two numbers on which the choice of operation is to be performed using Scanner class as follow:

After reading the inputs, switch case is performed using the choice variable (n). If it is 1 then, addition is perform and the sum is stored in the resultant variable (result) and comes out of the switch case.

If the choice is 2 then, the operation to be performed is subtraction. The difference of the two numbers is stored in resultant variable (result) and the execution comes out of the switch case.

If the operation of choice is 3 then, the we have to perform multiplication. The product of the two numbers is stored in the resultant variable (result) and we exit the switch case.

If the choice is 4 then, division has to be performed. For this, we first have to check if divisor is not zero. If it is zero division can’t be performed so we exit the switch case after equating result to zero else, we perform division and store in resultant variable (result) and then exit switch case.

If the choice is 5 then, we perform the modulus operation. The remainder of the two is stored in the resultant variable (result) and come out of the switch case.

After coming out of switch case, we display the output (result) on our console screen. This is repeated in loop until an invalid choice is entered. Then, we exit the entire while loop and come to the end of our code.

Источник

Java Program to Make a Calculator using Switch Case

In this Program we are making a simple calculator that performs addition, subtraction, multiplication and division based on the user input. The program takes the value of both the numbers (entered by user) and then user is asked to enter the operation (+, -, * and /), based on the input program performs the selected operation on the entered numbers using switch case.

If you are new to java, refer this Java tutorial to start learning java programming from basics.

Example: Program to make a calculator using switch case in Java

import java.util.Scanner; public class JavaExample < public static void main(String[] args) < double num1, num2; Scanner scanner = new Scanner(System.in); System.out.print("Enter first number:"); /* We are using data type double so that user * can enter integer as well as floating point * value */ num1 = scanner.nextDouble(); System.out.print("Enter second number:"); num2 = scanner.nextDouble(); System.out.print("Enter an operator (+, -, *, /): "); char operator = scanner.next().charAt(0); scanner.close(); double output; switch(operator) < case '+': output = num1 + num2; break; case '-': output = num1 - num2; break; case '*': output = num1 * num2; break; case '/': output = num1 / num2; break; /* If user enters any other operator or char apart from * +, -, * and /, then display an error message to user * */ default: System.out.printf("You have entered wrong operator"); return; >System.out.println(num1+" "+operator+" "+num2+": "+output); > >
Enter first number:40 Enter second number:4 Enter an operator (+, -, *, /): / 40.0 / 4.0: 10.0

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Comments

char ch = sc.next().charAt(0);
We have used sc next().charAt(0)
And where in integer we have used sc. nextInt()
I did not understand the use application of sc.next().charAt(0)

How do you add code where the user can re-enter the correct operator if they receive the error message?

i’m trying to make calculator similar to this one but with console program, is it possible to do it with console program.
if it’s possible what is the function that i can use instead of scanner

Источник

Java Program to Make a Simple Calculator Using switch. case

To understand this example, you should have the knowledge of the following Java programming topics:

Example: Simple Calculator using Java switch Statement

import java.util.Scanner; class Main < public static void main(String[] args) < char operator; Double number1, number2, result; // create an object of Scanner class Scanner input = new Scanner(System.in); // ask users to enter operator System.out.println("Choose an operator: +, -, *, or /"); operator = input.next().charAt(0); // ask users to enter numbers System.out.println("Enter first number"); number1 = input.nextDouble(); System.out.println("Enter second number"); number2 = input.nextDouble(); switch (operator) < // performs addition between numbers case '+': result = number1 + number2; System.out.println(number1 + " + " + number2 + " = " + result); break; // performs subtraction between numbers case '-': result = number1 - number2; System.out.println(number1 + " - " + number2 + " = " + result); break; // performs multiplication between numbers case '*': result = number1 * number2; System.out.println(number1 + " * " + number2 + " = " + result); break; // performs division between numbers case '/': result = number1 / number2; System.out.println(number1 + " / " + number2 + " = " + result); break; default: System.out.println("Invalid operator!"); break; >input.close(); > >
Choose an operator: +, -, *, or / * Enter first number 3 Enter second number 9 3.0 * 9.0 = 27.

Here, we have used the Scanner class to take 3 inputs from the user.

  • operator — specifies the operation to be performed
  • number1/number2 — operands to perform operation on

Since the operator matches the case ‘*’ , so the corresponding codes are executed.

result = number1 * number2; System.out.println(number + " * " + number2 + " /java-programming/break-statement">break statement ends the switch statement.

Similarly, for different operators, different cases are executed.

Output 2

Choose an operator: +, -, *, or / + Enter first number 21 Enter second number 8 21.0 + 8.0 = 29.0
Choose an operator: +, -, *, or / - Enter first number 9 Enter second number 3 9.0 - 3.0 = 6.0
Choose an operator: +, -, *, or / / Enter first number 24 Enter second number 8 24.0 / 8.0 = 3.0

Источник

Java Program to Make a Simple Calculator Using switch…case

In this post, we will learn to code the Java Program to Make a Simple Calculator Using switch…case. Let’s understand arithmetic operations in Java and about switch…case in Java Programming Language.

java program to make a simple calculator using switch case

In this program, we will use four basic arithmetic operators and take two operands from the user to make a simple calculator using switch case in Java Programming Language.

Operators will use to make the simple calculator in Java are as follows:

Operator Result
+ Addition of Two Numbers
Subtraction of Two Numbers
* Multiplication of Two Numbers
/ Division of Two Numbers

Now, Let’s see the code of the Java Program to Make a Simple Calculator Using switch…case.

Java Program to Make a Simple Calculator Using switch…case

import java.util.*; public class Main < public static void main(String[] args)< Scanner sc = new Scanner(System.in); System.out.println("Enter the Operator (+,-,*,/) : "); char operator = sc.next().charAt(0); System.out.println("Enter the First Operand : "); double first = sc.nextDouble(); System.out.println("Enter the Second Operand : "); double second = sc.nextDouble(); double result = 0; switch(operator)< case '+': result = first + second; break; case '-': result = first - second; break; case '*': result = first * second; break; case '/': result = first / second; break; >System.out.println("The Result is : \n "+first+" "+operator+" "+second+" wp-block-code">Enter the Operator (+,-,*,/) : + Enter the First Operand : 23 Enter the Second Operand : 45 The Result is : 23.0 + 45.0 = 68.0

Output 2

Enter the Operator (+,-,*,/) : / Enter the First Operand : 23 Enter the Second Operand : 45 The Result is : 23.0 / 45.0 = 0.5111111111111111

How Does This Program Work ?

Scanner sc = new Scanner(System.in); System.out.println("Enter the Operator (+,-,*,/) : "); char operator = sc.next().charAt(0); System.out.println("Enter the First Operand : "); double first = sc.nextDouble(); System.out.println("Enter the Second Operand : "); double second = sc.nextDouble();

In this program, we have taken the input of the operator and stored it in the variable operator of char data type by using the sc.next().charAt(0) function of the Scanner Class in Java. This is how we take input of character.

Then, we took the input of both the operands and stored it in the variable named first and second respectively using the Scanner class.

double result = 0; switch(operator)< case '+': result = first + second; break; case '-': result = first - second; break; case '*': result = first * second; break; case '/': result = first / second; break; >

Then, using the switch case in Java we created the simple calculator.

switch() – A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for a switch case.
When the operator matches a case, the statements following that case will execute until a break statement is reached.

Conclusion

I hope after going through this post, you understand Java Program to Make a Simple Calculator Using switch…case.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

  • Java Program to Add Two Complex Numbers
  • Java Program to Multiply Two Numbers
  • Java Program to Swap Two Numbers
  • Java Program to Find Factorial of a Number
  • Java Program to Check Prime Number

Источник

Java Program to Make a Simple Calculator Using switch. case

In this article, we will understand how to construct a simple calculator using switch-case. The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.

Following are the arithmetic operations we are going to perform.

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Floor Division
  • Modulo

Below is a demonstration of the same −

The two inputs: 40.0 and 12.0 Operator:%

The desired output would be −

The result is 40.0 % 12.0 = 4.0

Algorithm

Step 1 - START Step 2 - Declare three values namely my_input_1, my_input_2 and my_result and declare a character value namely operator. Step 3 - Read the required values from the user/ define the values Step 4 - Define case statements which takes ‘operator’ value as switch case to calculate the sum, difference, multiplication, division, modulus. Step 5 - Pass the operator value to the case statements to calculate the arithmetic operation between the two inputs ‘my_input_1’ and ‘my_input_2’ Step 7 - Display the result Step 8 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .

import java.util.Scanner; public class OperatorSwitch < public static void main(String[] args) < char operator; Double my_input_1, my_input_2, my_result; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.println("Enter the first number"); my_input_1 = my_scanner.nextDouble(); System.out.println("Enter the second number"); my_input_2 = my_scanner.nextDouble(); System.out.println("Enter any of the following operator: +, -, *, /, %"); operator = my_scanner.next().charAt(0); switch (operator) < case '+': my_result = my_input_1 + my_input_2; System.out.println(my_input_1 + " + " + my_input_2 + " = " + my_result); break; case '-': my_result = my_input_1 - my_input_2; System.out.println(my_input_1 + " - " + my_input_2 + " = " + my_result); break; case '*': my_result = my_input_1 * my_input_2; System.out.println(my_input_1 + " * " + my_input_2 + " = " + my_result); break; case '/': my_result = my_input_1 / my_input_2; System.out.println(my_input_1 + " / " + my_input_2 + " = " + my_result); break; case '%': my_result = my_input_1 % my_input_2; System.out.println(my_input_1 + " % " + my_input_2 + " = " + my_result); break; default: System.out.println("The operator you have selected is invalid"); break; >> >

Output

Required packages have been imported A reader object has been defined Enter the first number 40 Enter the second number 12 Choose any of the following operator: +, -, *, /, % % 40.0 % 12.0 = 4.0

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class OperatorSwitch < public static void main(String[] args) < char operator; Double my_input_1, my_input_2, my_result; my_input_1 = 40.0; my_input_2 = 12.0; operator = '%'; System.out.println("The two numbers are defined as " +my_input_1 +" and " +my_input_2); System.out.println("The operator is defined as " +operator); switch (operator) < case '+': my_result = my_input_1 + my_input_2; System.out.println(my_input_1 + " + " + my_input_2 + " = " + my_result); break; case '-': my_result = my_input_1 - my_input_2; System.out.println(my_input_1 + " - " + my_input_2 + " = " + my_result); break; case '*': my_result = my_input_1 * my_input_2; System.out.println(my_input_1 + " * " + my_input_2 + " = " + my_result); break; case '/': my_result = my_input_1 / my_input_2; System.out.println(my_input_1 + " / " + my_input_2 + " = " + my_result); break; case '%': my_result = my_input_1 % my_input_2; System.out.println(my_input_1 + " % " + my_input_2 + " = " + my_result); break; default: System.out.println("The operator you have selected is invalid"); break; >> >

Output

The two numbers are defined as 40.0 and 12.0 The operator is defined as % 40.0 % 12.0 = 4.0

Источник

Оцените статью